Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Answer: Running subroutines asynchronously

( #156603=categorized answer: print w/ replies, xml ) Need Help??

Q&A > subroutines > Running subroutines asynchronously contributed by crazyinsomniac

use Proc::Forkfunc;
use strict;

$|++; # autoflush/unbuffer

my @child_args = qw(1 2 );

forkfunc(\&child_func, @child_args);
forkfunc(\&child_func, 3);
forkfunc(\&child_func, 4);
forkfunc(\&child_func, 5);

sub child_func {
    # sleep for rand(3)*rand(3)
    select undef, undef, undef,  rand(3) * rand(3);

    print shift(@_);
    print "\n";
}
Proc::Forkfunc has the annoying habit of printing to STDERR "call to child returned", but it's not a complex module, and if you crack it open (look at the source), you can easily figure out what's goin on. You should also take a look at perlfork. This will work for most systems, but remember, forking is experimental on Win32 machines, and is not available before v5.6, but there is an alternative, Win32::Process.

Comment on Answer: Running subroutines asynchronously
Log In?
Username:
Password:

What's my password?
Create A New User
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (11)
As of 2014-10-22 12:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (118 votes), past polls