Skip to content

Instantly share code, notes, and snippets.

@preaction
Last active February 9, 2017 23:24
Show Gist options
  • Save preaction/973aa5603b0e167ebae3dbcd4e9a4b0d to your computer and use it in GitHub Desktop.
Save preaction/973aa5603b0e167ebae3dbcd4e9a4b0d to your computer and use it in GitHub Desktop.
use Mojo::Base -strict;
use Mojo::UserAgent;
use Mojo::IOLoop::Server;
my $JOBS = 5; # Keep this many requests in-flight
my @URLS = qw(
http://google.com http://google.com http://google.com http://google.com
http://google.com http://google.com http://google.com http://google.com
http://google.com http://google.com http://google.com http://google.com
http://google.com http://google.com http://google.com http://google.com
);
# Run the web scraper
my $ua = Mojo::UserAgent->new;
for my $i ( 1..$JOBS ) {
next_request( $ua, \@URLS );
}
# Open a TCP echo port
Mojo::IOLoop->server( { port => 7000 }, sub {
my ( $loop, $stream, $id ) = @_;
$stream->on(read => sub {
my ($stream, $bytes) = @_;
$stream->write( $bytes );
});
} );
# Wait 3 seconds, then send a message to the echo server
Mojo::IOLoop->timer( 3 => sub {
Mojo::IOLoop->client( { port => 7000 } => sub {
my ($loop, $err, $stream) = @_;
# Set up a listener before we write
$stream->on( read => sub {
my ( $stream, $bytes ) = @_;
print "Got response: " . $bytes . "\n";
} );
$stream->write( 'Hello, World' );
});
} );
exit Mojo::IOLoop->start;
sub next_request {
my ( $ua, $urls ) = @_;
my $url = shift @$urls;
$ua->get( $url => \&handle_response );
}
sub handle_response {
my ( $ua, $tx ) = @_;
# XXX Handle response
# Fire off another request
next_request( $ua );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment