Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

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?

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

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (11)
As of 2016-05-04 09:27 GMT
Find Nodes?
    Voting Booth?