Skip to content

Commit

Permalink
added delay recipe to cookbook
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Jan 12, 2012
1 parent 08309ae commit 8858c83
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 5 deletions.
2 changes: 1 addition & 1 deletion lib/Mojo/IOLoop.pm
Expand Up @@ -649,7 +649,7 @@ method is EXPERIMENTAL and might change without warning!
});
}
# Wait for events unless reactor is already running
# Wait for events if necessary
$delay->wait unless Mojo::IOLoop->is_running;
=head2 C<drop>
Expand Down
12 changes: 10 additions & 2 deletions lib/Mojo/IOLoop/Delay.pm
Expand Up @@ -49,7 +49,7 @@ Mojo::IOLoop::Delay - Synchronize events
});
}
# Wait for events unless reactor is already running
# Wait for events if necessary
$delay->wait unless Mojo::IOLoop->is_running;
=head1 DESCRIPTION
Expand Down Expand Up @@ -108,7 +108,15 @@ Decrement active event counter.
my @args = $delay->wait;
Start C<ioloop> and stop it again once the C<finish> event gets emitted.
Start C<ioloop> and stop it again once the C<finish> event gets emitted, only
works when C<ioloop> is not running already.
# Use the "finish" event to synchronize portably
$delay->on(finish => sub {
my ($delay, @args) = @_;
...
});
$delay->wait unless $delay->ioloop->is_running;
=head1 SEE ALSO
Expand Down
16 changes: 14 additions & 2 deletions lib/Mojolicious/Guides/Cookbook.pod
Expand Up @@ -704,7 +704,8 @@ You can take full control of the L<Mojo::IOLoop> reactor.
=head2 Parallel blocking requests

You can emulate blocking behavior by using a L<Mojo::IOLoop> delay to
synchronize multiple non-blocking requests.
synchronize multiple non-blocking requests. Just be aware that the resulting
transactions will be in random order.

# Synchronize non-blocking requests and capture result
my $ua = Mojo::UserAgent->new;
Expand All @@ -713,7 +714,18 @@ synchronize multiple non-blocking requests.
$ua->get('http://mojolicio.us/perldoc' => $delay->begin);
my ($tx, $tx2) = $delay->wait;

Just be aware that the resulting transactions will be in random order.
The C<finish> event of L<Mojo::IOLoop::Delay> can be used in code that needs
to be able to work standalone as well as inside L<Mojolicious> applications.

# Synchronize non-blocking requests portably
my $ua = Mojo::UserAgent->new;
my $delay = Mojo::IOLoop->delay(sub {
my ($ua, $tx, $tx2) = @_;
...
});
$ua->get('http://mojolicio.us' => $delay->begin);
$ua->get('http://mojolicio.us/perldoc' => $delay->begin);
$delay->wait unless Mojo::IOLoop->is_running;

=head2 Command line

Expand Down

0 comments on commit 8858c83

Please sign in to comment.