Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

help using the wait() command

by Ras (Acolyte)
on Feb 01, 2002 at 23:11 UTC ( [id://142801]=perlquestion: print w/replies, xml ) Need Help??

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

I understand that wait(); only waits for 1 process to
finish. But how can you make wait(); wait for mutilple
processes to finish?
thanks in advance.

Replies are listed 'Best First'.
Re: help using the wait() command
by VSarkiss (Monsignor) on Feb 01, 2002 at 23:27 UTC

    Actually, wait will return when one of the child processes exits, but it will "wait" (in some sense) on all of them.

    In other words, if you call wait and you have child processes that are executing, it will block. It will return when any one of them (or several if they happen to exit simultaneously) exits. If you fork off multiple children, and you want to block until all of them exit, just put wait in a loop: 1 while wait() > 0; More information at perlfunc:wait and wait.

    HTH

Re: help using the wait() command
by Zaxo (Archbishop) on Feb 02, 2002 at 01:52 UTC

    wait will sit and sleep until SIGCHLD triggers from any source. If you want to wait for a specific child, use waitpid.

    My pet idiom for waiting for multiple kids is this:

    my %family_bible = (); # @todo is an array of coderefs sub () {...;return $status} for (@todo) { my $kid; defined $kid = fork or last; $kid or exit &$_; $family_bible{$kid}++; } delete $family_bible{-wait} while %family_bible;
    If the kids need arguments, or you need to check their exit status, you'll need to modify the control structures. The family_bible hash makes the wait strategy insensitive to ordering (as it must be) and provides a convenient control structure for knowing when you're done.

    There are, of course, CPAN modules to handle all this for you. Parallel::ForkManager is one I recommend.

    After Compline,
    Zaxo

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (3)
As of 2024-04-24 21:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found