Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

Answer: Running subroutines asynchronously

by crazyinsomniac (Prior)
on Apr 04, 2002 at 08:01 UTC ( #156603=categorized answer: print w/replies, xml ) Need Help??

Q&A > subroutines > Running subroutines asynchronously - Answer 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 all is quiet...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (3)
As of 2018-03-19 01:53 GMT
Find Nodes?
    Voting Booth?
    When I think of a mole I think of:

    Results (231 votes). Check out past polls.