Skip to content

Commit

Permalink
added example for synchronizing multiple backend requests
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Jan 31, 2012
1 parent e318e2f commit 386b690
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Changes
@@ -1,6 +1,6 @@
This file documents the revision history for Perl extension Mojolicious.

2.47 2012-01-31 00:00:00
2.47 2012-02-01 00:00:00
- Added EXPERIMENTAL drain event to Mojo::Content.
- Added EXPERIMENTAL drain event to Mojo::Transaction::WebSocket.
- Added EXPERIMENTAL support for RSV1-3 flags to
Expand Down
32 changes: 32 additions & 0 deletions lib/Mojolicious/Guides/Cookbook.pod
Expand Up @@ -282,6 +282,38 @@ latency backend web services.
</body>
</html>

Multiple events such as parallel requests can be easily synchronized with a
L<Mojo::IOLoop> delay.

use Mojolicious::Lite;

# Search Twitter for "perl" and "python"
get '/' => sub {
my $self = shift;

# Delay rendering
my $delay = Mojo::IOLoop->delay(sub {
my ($delay, @results) = @_;
$self->render(json => {results => \@results});
});

# First request
$delay->begin;
$self->ua->get('http://search.twitter.com/search.json?q=perl' => sub {
my ($ua, $tx) = @_;
$delay->end($tx->res->json->{results}->[0]->{text});
});

# Second request
$delay->begin;
$self->ua->get('http://search.twitter.com/search.json?q=python' => sub {
my ($ua, $tx) = @_;
$delay->end($tx->res->json->{results}->[0]->{text});
});
};

app->start;

=head2 Timers

Another primary feature of the L<Mojo::IOLoop> reactor are timers, which can
Expand Down

0 comments on commit 386b690

Please sign in to comment.