Skip to content

Commit

Permalink
simplify non-blocking Mojo::UserAgent recipe a little
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Nov 8, 2012
1 parent 8b30830 commit af0d38b
Showing 1 changed file with 4 additions and 5 deletions.
9 changes: 4 additions & 5 deletions lib/Mojolicious/Guides/Cookbook.pod
Expand Up @@ -936,18 +936,17 @@ can keep many parallel connections active at the same time.

# Crawler
sub crawl {
my $id = shift;

# Dequeue or wait 2 seconds for more URLs
return Mojo::IOLoop->timer(2 => sub { crawl($id) })
return Mojo::IOLoop->timer(2 => sub { crawl() })
unless my $url = shift @urls;

# Fetch non-blocking just by adding a callback
$ua->get($url => sub {
my ($ua, $tx) = @_;

# Extract URLs
say "[$id] $url";
say $url;
$tx->res->dom('a[href]')->each(sub {
my $e = shift;

Expand All @@ -960,12 +959,12 @@ can keep many parallel connections active at the same time.
});

# Next
crawl($id);
crawl();
});
};

# Start a bunch of parallel crawlers sharing the same user agent
crawl($_) for 1 .. 3;
crawl() for 1 .. 3;

# Start event loop if necessary
Mojo::IOLoop->start unless Mojo::IOLoop->is_running;
Expand Down

0 comments on commit af0d38b

Please sign in to comment.