Non-blocking writes and chunked output #2282
-
|
I would like to write an HTTP server using Mojolicious that performs non-blocking I/O and chunked transfer encoding. Looking at the example at https://docs.mojolicious.org/Mojolicious/Guides/Rendering#Chunked-transfer-encoding This doesn't seem to run: With my own code, nothing is sent to the client until the sub returns: The example I referred to at https://docs.mojolicious.org/Mojolicious/Guides/Rendering#Chunked-transfer-encoding states How exactly do you achieve this with Mojolicious, please? I saw a similar question at #2260 but without a direct solution. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
|
It looks like something got lost when creating this discussion - this line from strace: ... shows that your mojo app is crashing out with an exception, When I take your example and remove the prototype from off the callback to @kraih Looking at the code, this might actually be a doc bug? The docs at/around here mojo/lib/Mojolicious/Controller.pm Line 897 in 75508c2 ... shows the sub with a one-item prototype: sub ($c) {...}
but sub write_chunk {
my ($self, $chunk, $cb) = @_;
$self->res->content->write_chunk($chunk, $cb ? sub { shift; $self->$cb(@_) } : ());
return $self->rendered;
}which is effectively: $cb->($self, $chunk, $cb)and that's not compatible with a ... so maybe the docs shouldn't prototype that code-ref? @richardp345 Also, your use of |
Beta Was this translation helpful? Give feedback.
-
|
Thank you! It does look like a mistake in the docs. WIthout the proto the code runs. N.B. the sleep is a contrived example of a synchronous call. The real use case is to read from a DB. |
Beta Was this translation helpful? Give feedback.
It looks like something got lost when creating this discussion - this line from strace:
... shows that your mojo app is crashing out with an exception,
and that the exception is being printed to STDERR, but it isn't in the output you pasted.
When I take your example and remove the prototype from off the callback to
write_chunk,that error goes away...
and I get the head and body text back with
carton exec -- perl ./your-software get /.@kraih Looking at the code, this might acβ¦