what is Mojo::IOLoop->is_running waiting for?

67 views
Skip to first unread message

Mario Gee

unread,
Jun 30, 2015, 3:22:16 AM6/30/15
to mojol...@googlegroups.com
Hi 

I am trying to make concurrent requests like this:

use 5.016;
use Mojo::UserAgent;

my $ua = Mojo::UserAgent->new;
say "starting " . scalar localtime();

my $tx = Mojo::Transaction::HTTP->new;
$tx->req->method('GET');
$tx->req->url->parse('http://www.google.com/');
$ua->start($tx => \&mojo_callback);

$tx = Mojo::Transaction::HTTP->new;
$tx->req->method('GET');
$tx->req->url->parse('http://www.google.com/');
$ua->start($tx => \&mojo_callback);

sub mojo_callback{
  my ($ua, $tx) = @_;
  say $tx->res->code . " " .  scalar localtime() ;
}

Mojo::IOLoop->start unless Mojo::IOLoop->is_running;

say "after is_running " . scalar localtime();


But is_running is taking up to 20 seconds until finish.

How can I enqueue 2 concurrent requests and after both ended immediatly continue?

cheers

Ben van Staveren

unread,
Jun 30, 2015, 3:30:19 AM6/30/15
to mojol...@googlegroups.com
Hmm, like this maybe:

use strict;
use warnings;
use Mojo::UserAgent;
use Mojo::IOLoop;

my $ua = Mojo::UserAgent->new;

my $delay = Mojo::IOLoop->delay(sub {
  my $delay = shift;  
  exit(0); 
});

foreach my $url ('http://www.google.com/', 'http://www.google.com/') {
  my $end = $delay->begin(0);
  $ua->get($url => sub {
    say $tx->res->code . ' ' . scalar(localtime());
    $end->();
  });
} 

Mojo::IOLoop->start unless Mojo::IOLoop->is_running;
--

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 mojolicious...@googlegroups.com.

To post to this group, send email to mojol...@googlegroups.com.

Visit this group at http://groups.google.com/group/mojolicious.

For more options, visit https://groups.google.com/d/optout.






Dan Book

unread,
Jun 30, 2015, 9:08:12 AM6/30/15
to mojol...@googlegroups.com
is_running isn't what takes 20 seconds, Mojo::IOLoop->start blocks until the event loop is stopped/has nothing more to do. So it waits for your requests to complete. Delays are an easier option to synchronize non-blocking requests, see https://metacpan.org/pod/Mojolicious::Guides::Cookbook#Concurrent-blocking-requests

-Dan

Mario Gee

unread,
Jun 30, 2015, 9:55:44 AM6/30/15
to mojol...@googlegroups.com
thanx,
I tried Mojo::IOLoop->delay as well. But I did not find a way to queue the request in order to limit the maximum allowed concurrent request spawned. (being polite to the web server). Is queue even the right pattern for that? Sorry I am very new to mojo and do not know the framework well.
Reply all
Reply to author
Forward
0 new messages