Skip to content

Instantly share code, notes, and snippets.

@diegok
Created March 24, 2012 09:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save diegok/2180712 to your computer and use it in GitHub Desktop.
Save diegok/2180712 to your computer and use it in GitHub Desktop.
Parallel GET requests into a timer of a mojolicious::lite app example
#!/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