Looking for a method to kill all children for my Timeout::Errorcontingency

Discussion in 'Ruby' started by Xeno Campanoli, Mar 24, 2009.

  1. Do I really need to collect all the pids from fork to kill them in that case, or
    is there a method I'm not finding in pickaxe that just does it???

    Xeno Campanoli, Mar 24, 2009
    1. Advertisements

  2. Xeno Campanoli

    Jos Backus Guest

    Here's a trick I use, maybe it is useful. Note that even if push-to-slaves
    creates children, those too will be killed because they reside in the same
    process group.

    require 'timeout'
    signal = '-TERM'
    timeout = 30

    pid = Process.fork do
    exec %{push-to-slaves -v}
    Timeout::timeout(timeout) { Process.waitpid(pid) }
    rescue Timeout::Error => exc
    puts "#{exc.message}, killing pid #{pid}"
    Process.kill(signal, pid)
    res = Process.waitpid(pid)
    puts "pid #{pid.inspect} != res #{res.inspect}" if res != pid
    Jos Backus, Mar 24, 2009
    1. Advertisements

  3. The problem with that is I have many processes, so I would be doing this with
    arrays and loops, which suddenly becomes problematic. If I could just call
    something that automatically kills the entire tree of children on down that
    would be much nicer...??
    Xeno Campanoli, Mar 24, 2009
  4. Xeno Campanoli

    Mike B Guest

    Maybe this could help?


    It's a link to Ara Howards announcement for terminator.

    Mike B.
    Mike B, Mar 25, 2009
  5. Xeno Campanoli

    Jos Backus Guest

    If those many processes were being started by the push-to-slaves script as in
    the example I posted, unless that script did something to create new process
    groups for its children, the code I posted would terminate all those processes
    because they all belong to the same process group which is targeted by the
    Process.kill(-TERM, pid) invocation. Iow, if you want to kill all the children
    at once the easiest way is to make sure they all belong to the same pgrp.
    Jos Backus, Mar 25, 2009
  6. Yes thank you.

    Part of my problem is I wasn't feeling sure about how I would use processes or
    threads, and why. With the advent of the new Chrome Philosophy, I was thinking
    perhaps processes might be the way to go, as I was doing curl executions, but it
    turns out I need to do a surrounding sequence of things in parallel for
    initializations and closures, so I am leaning back to threads now, which I think
    solves the problems. I would sure like to find a book just on use of threads
    and processes in Ruby. I would think a knowledgeable person could probably write
    at least 12 chapters on it for the layman alone, and not having done some of
    this for over ten years, that's where I'm feeling I'm at.

    Thank you again for the feedback.

    Sincerely, Xeno

    Xeno Campanoli, Mar 25, 2009
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.