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


In reply to What is this "Do you need to predeclare croak" about? [SOLVED] by karlgoethebier

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.