Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > waiting for multiple child processes to finish

Reply
Thread Tools

waiting for multiple child processes to finish

 
 
Martin DeMello
Guest
Posts: n/a
 
      11-13-2006
Why does this not work (it waits for each process to finish before
launching the next):
-------------------------------------------------------------
$stdout.sync = true

10.times {|i|
pid = Process.fork {
puts "launching process #{i}"
3.times {
sleep 0.5
puts "hello from process #{i}"
}
}
Thread.new { Process.waitpid(pid) }.join
}

puts "done!"
-------------------------------------------------------------

but this does:
-------------------------------------------------------------
$stdout.sync = true

pids = []
10.times {|i|
pids << Process.fork {
puts "launching process #{i}"
3.times {
sleep 0.5
puts "hello from process #{i}"
}
}
}

pids.each {|pid| Thread.new { Process.waitpid(pid) }.join }
puts "done!"
-------------------------------------------------------------

 
Reply With Quote
 
 
 
 
Farrel Lifson
Guest
Posts: n/a
 
      11-13-2006
On 13/11/06, Martin DeMello <(E-Mail Removed)> wrote:
> Why does this not work (it waits for each process to finish before
> launching the next):
> -------------------------------------------------------------
> $stdout.sync = true
>
> 10.times {|i|
> pid = Process.fork {
> puts "launching process #{i}"
> 3.times {
> sleep 0.5
> puts "hello from process #{i}"
> }
> }
> Thread.new { Process.waitpid(pid) }.join
> }
>
> puts "done!"
> -------------------------------------------------------------
>
> but this does:
> -------------------------------------------------------------
> $stdout.sync = true
>
> pids = []
> 10.times {|i|
> pids << Process.fork {
> puts "launching process #{i}"
> 3.times {
> sleep 0.5
> puts "hello from process #{i}"
> }
> }
> }
>
> pids.each {|pid| Thread.new { Process.waitpid(pid) }.join }
> puts "done!"
> -------------------------------------------------------------


The join in the first solution forces the thread (and forked process)
to finish before continuing onto starting the next fork. In the second
solution all the processes are forked first before waiting. You can
probably rewrite that last loop simply as:
pids.each{|pid| Process.waitpid(pid)}

Farrel

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Print digital photos online - 9 cents per print, plus 10% off. All sizes from 4x6 up to 30x60. Same price for Matte finish and glossy finish nathan_usny Digital Photography 2 09-12-2005 11:30 PM
Print digital photos online - 9 cents per print, plus 10% off. All sizes from 4x6 up to 30x60. Same price for Matte finish and glossy finish nathan_usny Digital Photography 0 09-12-2005 06:09 PM
waiting for external process to finish (using Runtime and Thread classes) brownjenkn@aol.com Java 1 04-22-2005 07:55 PM
How do I: Main thread spawn child threads, which child processes...control those child processes? Jeff Rodriguez C Programming 23 12-09-2003 11:06 PM
Waiting for processes to finish under Solaris Behrang Dadsetan Python 1 07-15-2003 07:56 PM



Advertisments