Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

AnyEvent::Fork not working as expected

by davies (Prior)
on Jun 28, 2021 at 18:26 UTC ( [id://11134405]=perlquestion: print w/replies, xml ) Need Help??

davies has asked for the wisdom of the Perl Monks concerning the following question:

I am trying to get one of the examples from the official documentation working. I accept that putting code snippets together has problems, but I have tried lots of variants without success. The example is about 75% of the way through the document and starts with my $pool = AnyEvent::Fork. My current version of it is:

use strict; use warnings; use AnyEvent; use AnyEvent::Fork; use feature 'say'; package Some; # Some::function might look like this - all parameters passed before f +ork # and after will be passed, in order, after the communications socket. sub funct{ my ($fh, $str1, $str2, $fh1, $fh2, $str3) = @_; print scalar <$fh>; # prints "hi #1\n" and "hi #2\n" in any order } package main; my $pool = AnyEvent::Fork ->new ->send_arg ("str1", "str2") ; for (1..2) { $pool ->fork ->send_arg ("str3") ->run ("Some::funct", sub { say 'Inside anonysub'; }); } say 'Outside pool loop'; AnyEvent->condvar->recv;

Thjis goes into an infinite loop after printing the output below:

Outside pool loop Inside anonysub Inside anonysub Goto undefined subroutine &Some::funct at /usr/local/share/perl/5.24.1 +/AnyEvent/Fork/Serve.pm line 87. Goto undefined subroutine &Some::funct at /usr/local/share/perl/5.24.1 +/AnyEvent/Fork/Serve.pm line 87.

I'm not worried about the infinite loop - at least not yet - but the inability to find a fully qualified sub (what the docs say is required) puzzles me. Could someone please give me a pointer?

Regards,

John Davies

Replies are listed 'Best First'.
Re: AnyEvent::Fork not working as expected
by jo37 (Deacon) on Jun 28, 2021 at 20:12 UTC

    It looks like AnyEvent::Fork insists in require'ing a module. Separating the Some package into a .pm file makes the "undefined subroutine" go away, though it results in some "uninitialized value" errors.

    use lib '.'; my $pool = AnyEvent::Fork ->new ->require("Some") ->send_arg ("str1", "str2") ;

    Greetings,
    -jo

    $gryYup$d0ylprbpriprrYpkJl2xyl~rzg??P~5lp2hyl0p$
Re: AnyEvent::Fork not working as expected
by Aldebaran (Curate) on Jul 01, 2021 at 06:07 UTC
    I have tried lots of variants without success.

    So have I with fork scripts, and I haven't come up with anything satifactory. I received this warning for several of M Lehmann's modules including this one:

    *** The stability canary says: (nothing, it was driven away by harsh w +eather) *** *** It seems you are running perl version 5.030000, likely the "offici +al" or *** "standard" version. While there is nothing wrong with doing that, *** standard perl versions 5.022 and up are not supported by JSON::XS. *** While this might be fatal, it might also be all right - if you run + into *** problems, you might want to downgrade your perl or switch to the *** stability branch. *** *** If everything works fine, you can ignore this message.

    I tried to let it run its course but eventually control-c'ed and q'ed my way out of it. I'll just chalk it up as one of many failed attempts to use some type of fork in perl. Instead of trying again, I should probably just accept that perl is uniprocess by design and not use a hammer as a screwdriver.

      The easiest and recommended solution is not to use modules by the mentioned author. For the mentioned module, JSON::XS, an alternative exists: Cpanel::JSON::XS.

      map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11134405]
Approved by marto
Front-paged by marto
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (7)
As of 2024-04-19 10:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found