Skip to content

Commit

Permalink
more detailed delay examples
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Mar 25, 2012
1 parent b827e4c commit a13244a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
22 changes: 18 additions & 4 deletions lib/Mojo/UserAgent.pm
Expand Up @@ -585,7 +585,10 @@ Mojo::UserAgent - Non-blocking I/O HTTP 1.1 and WebSocket user agent
$ua->max_redirects(5)->get('latest.mojolicio.us')
->res->content->asset->move_to('/Users/sri/mojo.tar.gz');
# Parallel requests
# TLS certificate authentication
my $tx = $ua->cert('tls.crt')->key('tls.key')->get('https://mojolicio.us');
# Blocking parallel requests (does not work inside a running event loop)
my $delay = Mojo::IOLoop->delay;
for my $url ('mojolicio.us', 'cpan.org') {
$delay->begin;
Expand All @@ -596,10 +599,21 @@ Mojo::UserAgent - Non-blocking I/O HTTP 1.1 and WebSocket user agent
}
my @titles = $delay->wait;
# TLS certificate authentication
my $tx = $ua->cert('tls.crt')->key('tls.key')->get('https://mojolicio.us');
# Non-blocking parallel requests (does work inside a running event loop)
my $delay = Mojo::IOLoop->delay(sub {
my ($delay, @titles) = @_;
...
});
for my $url ('mojolicio.us', 'cpan.org') {
$delay->begin;
$ua->get($url => sub {
my ($ua, $tx) = @_;
$delay->end($tx->res->dom->at('title')->text);
});
}
$delay->wait unless Mojo::IOLoop->is_running;
# WebSocket request
# Non-blocking WebSocket connection
$ua->websocket('ws://websockets.org:8787' => sub {
my ($ua, $tx) = @_;
$tx->on(finish => sub { say 'WebSocket closed.' });
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojolicious/Guides/Cookbook.pod
Expand Up @@ -928,7 +928,7 @@ 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) = @_;
my ($delay, $tx, $tx2) = @_;
...
});
$ua->get('http://mojolicio.us' => $delay->begin);
Expand Down

0 comments on commit a13244a

Please sign in to comment.