Created
October 27, 2012 21:18
-
-
Save sirg/3966312 to your computer and use it in GitHub Desktop.
Don't get Mojo::IOLoop
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
use Mojolicious::Lite; | |
sub gdone { | |
my $tx = shift; | |
$tx->res->dom->at('.section-en_us-tc .titletext')->text; | |
} | |
sub ydone { | |
my $tx = shift; | |
$tx->res->dom->at('.weather-data .temp-range')->text; | |
} | |
sub dchain { | |
my ($sub, $chain) = @_; | |
sub { | |
my ($delay, $arg) = @_; | |
my $res = $sub->($arg); | |
$delay->$chain($res); | |
} | |
} | |
get '/' => sub { | |
my $self = shift; | |
my $ua = app->ua; | |
Mojo::IOLoop->delay( | |
sub { | |
my $delay = shift; | |
$ua->get('www.yahoo.com' => dchain(\&ydone, $delay->begin)); | |
$ua->get('news.google.com' => dchain(\&gdone, $delay->begin)); | |
}, | |
sub { | |
my ($d, $res_y, $res_g) = @_; | |
$self->render(text => "'$res_y' '$res_g'"); | |
}); | |
}; | |
app->start; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Mojo::IOLoop already running at /home/sergey/perl5/perlbrew/perls/perl-5.16.1/lib/site_perl/5.16.1/Mojo/IOLoop/Delay.pm line 27. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Solution: use multiple steps as args to delay instead of calling wait().
Notice that post-processing can be chained onto completion of the action before calling the step completion callback. This allows complex/costly post-processing to be started before the next step.