I'm working in a section of a Perl module that creates a large CSV response. The server runs on Plack, on which I'm far from expert.
Currently I'm using something like this to send the response:
$res->content_type('text/csv');
my $body = '';
query_data (
parameters => \%query_parameters,
callback => sub {
my $row_object = shift;
$body .= $row_object->to_csv;
},
);
$res->body($body);
return $res->finalize;
However, that query_data
function is not a fast one and retrieves a lot of records. In there, I'm just concatenating each row into $body
and, after all rows are processed, sending the whole response.
I don't like this for two obvious reasons: First, it takes a lot of RAM until $body
is destroyed. Second, the user sees no response activity until that method has finished working and actually sends the response with $res->body($body)
.
I tried to find an answer to this in the documentation without finding what I need.
I also tried calling $res->body($row_object->to_csv)
on my callback section, but seems like that ends up sending only the last call I made to $res->body
, overriding all previous ones.
Is there a way to send a Plack response that flushes the content on each row, so the user starts receiving content in real time as the data is gathered and without having to accumulate all data into a veriable first?
Thanks in advance for any comments!
You can't use Plack::Response because that class is intended for representing a complete response, and you'll never have a complete response in memory at one time. What you're trying to do is called streaming, and PSGI supports it even if Plack::Response doesn't.
Here's how you might go about implementing it (adapted from your sample code):
my $env = shift;
if (!$env->{'psgi.streaming'}) {
# do something else...
}
# Immediately start the response and stream the content.
return sub {
my $responder = shift;
my $writer = $responder->([200, ['Content-Type' => 'text/csv']]);
query_data(
parameters => \%query_parameters,
callback => sub {
my $row_object = shift;
$writer->write($row_object->to_csv);
# TODO: Need to call $writer->close() when there is no more data.
},
);
};
Some interesting things about this code:
Plack::Response
object, you can return a sub
. This subroutine will be called some time later to get the actual response. PSGI supports this to allow for so-called "delayed" responses.coderef
(in this case, $responder
) that should be called and passed the real response. If the real response does not include the "body" (i.e. what is normally the 3rd element of the arrayref
), then $responder
will return an object that we can write the body to. PSGI supports this to allow for streaming responses.$writer
object has two methods, write
and close
which both do exactly as their names suggest. Don't forget to call the close
method to complete the response; the above code doesn't show this because how it should be called is dependent on how query_data
and your other code works.$env->{'psgi.streaming'}
to be sure that yours does.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With