Created
March 24, 2012 09:50
-
-
Save diegok/2180712 to your computer and use it in GitHub Desktop.
Parallel GET requests into a timer of a mojolicious::lite app example
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; | |
use Mojo::IOLoop; | |
my $clients = []; | |
my $timer_id; | |
set_next_requests(); | |
get '/' => sub { shift->render( text => "I'm alive!" ) }; | |
websocket '/get-info' => sub { | |
my $self = shift; | |
push @$clients, $self; | |
}; | |
app->start; | |
sub set_next_requests { | |
my $timer_id = Mojo::IOLoop->timer(5 => sub { | |
my $delay = Mojo::IOLoop->delay(sub { | |
my ($delay, @txs) = @_; | |
got_all_urls( @txs ); | |
set_next_requests(); | |
}); | |
my @urls = ('http://mojolicio.us', 'http://mojolicio.us/perldoc'); | |
for my $url ( @urls ) { | |
$delay->begin; | |
app->ua->get( $url => sub { $delay->end(pop) }); | |
} | |
}); | |
} | |
sub got_all_urls { | |
for my $tx ( @_ ) { | |
app->log->debug( $tx->res->dom->at('title')->text ); | |
} | |
# here you push whatever to $clients | |
for my $client ( @$clients ) { | |
$client->send('ok'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment