Permalink
Browse files

added delay recipe to cookbook

  • Loading branch information...
1 parent 08309ae commit 8858c83d44274ba392155dfc7cbcf09186f7571b @kraih kraih committed Jan 12, 2012
Showing with 25 additions and 5 deletions.
  1. +1 −1 lib/Mojo/IOLoop.pm
  2. +10 −2 lib/Mojo/IOLoop/Delay.pm
  3. +14 −2 lib/Mojolicious/Guides/Cookbook.pod
View
2 lib/Mojo/IOLoop.pm
@@ -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>
View
12 lib/Mojo/IOLoop/Delay.pm
@@ -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
@@ -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
View
16 lib/Mojolicious/Guides/Cookbook.pod
@@ -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;
@@ -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

0 comments on commit 8858c83

Please sign in to comment.