karlgoethebier has asked for the wisdom of the Perl Monks concerning the following question:
Hi all,
i wrote this:
#!/usr/bin/env perl use strict; use warnings; use threads; use MCE::Loop; use MCE::Shared; use LWP::Simple; use feature qw(say); use constant AMOUNT => 0; my $result = MCE::Shared->hash; my @urls = qw(http://perlmonks.org http://www.whitehouse.org ); MCE::Loop::init { max_workers => 'auto', chunk_size => 1 }; my $fetch = sub { head(shift) }; mce_loop { my @data = $fetch->($_); sleep AMOUNT; $result->set( $_ => \@data ); } @urls; { no warnings qw(uninitialized); my $iter = $result->iterator(); while ( my ( $url, $data ) = $iter->() ) { say $url; say for @$data; say q(---); } } __END__
It works as expected but sometimes i get this confusing error message:
String found where operator expected at /Users/karl/perl5/perlbrew/per +ls/perl-5.24.1threads/lib/5.24.1/darwin-thread-multi-2level/IO/Socket +/INET.pm line 303, near "croak 'usage: $sock->peerhost()'" (Do you need to predeclare croak?) # ... many more like this
This comes from...
sub peerhost { @_ == 1 or croak 'usage: $sock->peerhost()'; my($sock) = @_; my $addr = $sock->peeraddr; $addr ? inet_ntoa($addr) : undef; }
... and many more subs in INET.pm
Update: Replaced $_ with shift. Sorry.
Update2: Mh, it seems when i don't use threads the phenomenon doesn't occur...
Update3: From time to time i get this:
Segmentation fault: 11 Deep recursion on subroutine "IO::Socket::new" at /Users/karl/perl5/pe +rlbrew/perls/perl-5.24.1threads/lib/5.24.1/IO/Socket/IP.pm line 353, +<__ANONIO__> line 2.
Update4: Switching to WWW::Curl::Easy made it work:
#!/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
Thanks to all fellow monks for help.
Thanks for any hint and best regards, Karl
«The Crux of the Biscuit is the Apostrophe»
Furthermore I consider that Donald Trump must be impeached as soon as possible
|
---|