Thanks Dotan

I got a "premature connection close" even before adding the sleep call. 
That's why I added a sleep to give a chance to my code to wait enough time 
for the HTTP requests to get a response. I'm not sure how to make my code 
wait for this response another way. 

Le mardi 4 février 2014 07:02:27 UTC+1, Dotan Dimet a écrit :
>
>  sleep() is a BLOCKING system call, so your program will just block all 
> execution for 10 seconds when it reaches that line, not wait for anything 
> to happen. This will mess up with all the non-blocking code - as far as the 
> UA is concerned, you started a request and then hung the program, which I 
> think is why you get a premature connection close.
> You are also doing this inside a recurring call, so the IOLoop will try to 
> start another of these every 20 seconds - each of them blocking the program 
> for 10 seconds.
> Also, if you run this by starting a server (morbo or daemon), you might 
> want to print to STDERR, not to STDOUT. 
>
> On 2/4/14, 7:24 AM, Vincent HETRU wrote:
>  
>  Hi, 
>
>  In the UserAgent's doc
>
>  I read that I can do Non-blocking HTTP requests that does work inside a 
> running event loop.
>
>  So i thought I could run the example code of the documentation inside 
> the callback of a recurring call like this:
>
>  use Mojo::Base 'Mojolicious';
>> use Data::Dumper;
>> use Mojo::IOLoop;
>> # This method will run once at server start
>> sub startup {
>>   my $self = shift;
>>   my $timer_id = Mojo::IOLoop->recurring(20 => sub {
>>     print("enter timer\n");
>>     # Non-blocking parallel requests (should work inside a running event 
>> loop according to the doc)
>>     my $ua = Mojo::UserAgent->new;
>>     my $delay = Mojo::IOLoop->delay(sub {
>>       my ($delay, @titles) = @_;
>>       print (Dumper(@titles));
>>     });
>>     for my $url ('mojolicio.us', 'cpan.org') {
>>       my $end = $delay->begin(0);
>>       $ua->get($url => sub {
>>         my ($ua, $tx) = @_;
>>         $end->($tx->res->dom->at('title')->text);
>>       });
>>     }
>>     $delay->wait unless Mojo::IOLoop->is_running;
>>     sleep(10);
>>     print("leave timer\n");
>>   });
>> }
>
>
>  but it does not work as expected, and I got a "Premature connection 
> close" error message for both http requests. I put the sleep(10); to make 
> sure I gave enough time for both requests to get through, but that doesn't 
> help.
>
>  What I am missing or doing wrong?
>
>  Best Regards.
>
>  Vincent
>  -- 
> 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] <javascript:>.
> To post to this group, send email to [email protected]<javascript:>
> .
> Visit this group at http://groups.google.com/group/mojolicious.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>
> 

-- 
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 http://groups.google.com/group/mojolicious.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to