When running non-blocking requests, the user agent object must be stored
somewhere in a larger scope so that it is not destroyed before the request
completes. See also
https://metacpan.org/pod/Mojolicious::Guides::FAQ#What-does-%22Premature-connection-close%22-mean
?

On Tue, Mar 28, 2017 at 1:20 AM, Jeremy Begg <[email protected]>
wrote:

> Hi,
>
> I am trying to persuade Mojolicious to initiate four HTTP requests and
> then inspect the results of each when they finish.
> Ideally the program will
> 1. Set up four concurrent HTTP requests
> 2. Wait for all four to finish
> 3. Process the results.
>
> I've read the documentation for Mojo::IOLoop, Mojo::IOLoop::Delay and
> Mojo::UserAgent.  All of them contain examples which indicate how this
> might be done, but in all the variations I have tried my code fails when it
> tries to extract the HTTP result bodypart.
> The error message is variations on "Premature connection close".
>
> Here is the relevant portion of my code as it stands now.  It's based on
> the Mojo::UserAgent example titled "Concurrent non-blocking requests
> (synchronized with a delay)".
> The dblist hash has is simply a way of tracking a set of unique URLs
> (currently four).
>
>
>         my @results;
>
>         Mojo::IOLoop->new->delay(
>             # Set up the requests
>             sub {
>                 my $delay = shift;
>                 foreach my $db (keys %dblist) {
>                     print "\nSending request $url$db&q=$query\n";
>                     my $HTTP = Mojo::UserAgent->new;
>                     $HTTP->get("$url$db&q=$query" => $delay->begin);
>                 }
>             },
>             # Save the results
>             sub {
>                 my $delay = shift;
>                 print "Saving ".scalar(@_)." results...\n";
>                 foreach my $tx (@_) {
>                     print "Got a result?\n";
>                     push @results, $tx->result->body;
>                 }
>             }
>         )->wait;
>
>
>         print "\nReceived ", scalar(@results), " results\n";
>         print " ->    $_\n" foreach @results;
>
> When I run the program (using the Mojo Test harness) I get:
>
>
> Sending request http://localhost:8086/query?epoch=s&db=solarmon_day&q=...
>
> Sending request http://localhost:8086/query?epoch=s&db=solarmon_30min&q=..
> .
>
> Sending request http://localhost:8086/query?epoch=s&db=solarmon_5min&q=...
>
> Sending request http://localhost:8086/query?epoch=s&db=solarmon&q=...
> Saving 4 results...
> Got a result?
> Mojo::Reactor::Poll: Timer failed: Premature connection close at myprogram
> .pm line 1042.
>
> Received 0 results
>
> (I have removed the details of the full URL request path for brevity.)
>
> Line 1042 is the *push* statement.
>
> I think the HTTP request itself is correct; if a issue a single,
> non-blocking request using any of the four URLs I get a valid response.
>
> I've been banging my head against this for a couple of days now and I
> don't seem to be be getting closer to a result.
> Help!
>
> Thanks,
>
> Jeremy Begg
>
> --
> You received this message because you are subscribed to the Google Groups
> "Mojolicious" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To post to this group, send email to [email protected].
> Visit this group at https://groups.google.com/group/mojolicious.
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Mojolicious" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/mojolicious.
For more options, visit https://groups.google.com/d/optout.

Reply via email to