#!/usr/bin/env perl # The sky may fall on your head tomorrow, but tomorrow never comes # $Id: uagent.pl,v 1.5 2017/06/15 13:03:32 karl Exp karl $ use strict; use warnings; use threads; use MCE::Loop; use MCE::Shared; use MCE::Mutex; # who knows what happens ;-) use WWW::Curl::Easy; use feature qw(say); use constant AMOUNT => 0.008; my $result = MCE::Shared->hash; my @urls = qw(http://perlmonks.org http://www.whitehouse.org ); MCE::Loop::init { max_workers => 'auto', chunk_size => 1, interval => AMOUNT, # posix_exit => 1, # useless when loading threads }; my $fetch = sub { my $curl = WWW::Curl::Easy->new; my ( $header, $body ); $curl->setopt( CURLOPT_URL, shift ); $curl->setopt( CURLOPT_WRITEHEADER, \$header ); $curl->setopt( CURLOPT_WRITEDATA, \$body ); $curl->perform; $header; }; my $mutex = MCE::Mutex->new; mce_loop { MCE->yield; my $data = $fetch->($_); $data =~ s/\n+$/---/; $mutex->enter( $result->set( $_ => $data ) ); } @urls; my $iter = $result->iterator(); while ( my ( $url, $data ) = $iter->() ) { say qq($url\n$data); } __END__ # for i in {1..100}; do ./uagent.pl; done