Thread popen and broken pipe problem

Discussion in 'Ruby' started by L., Sep 15, 2008.

  1. L.

    L. Guest

    Hello there, I try to execute a list of command in a thread thanks to
    popen.
    But during command execution user may wants to interrupt / kill the
    execution process.
    My code looks like that :

    @thread = Thread.new do
    @command_list.each {|name, cmd|
    IO.popen(cmd){ |@pipe|
    pipe.each_line {|l|
    puts l
    }
    }
    }
    end

    But when i try to @thread.kill I have a broken pipe error message on
    the current command execution.

    How to correctly finish and kill the thread without having such
    errors?

    thanks.
    L., Sep 15, 2008
    #1
    1. Advertising

  2. L.

    L. Guest

    Found it,
    when io is set after popen use, it's easy to get the process ID and
    then to correctly kill it during its execution.


    #get the process id

    IO.popen(cmd){ |pipe|
    current_process = pipe.id # Saving the PID
    pipe.each_line {|l|
    puts l
    }

    #kill the process

    Process.kill("KILL", current_process)

    et voila.
    L., Sep 15, 2008
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Tom Brown
    Replies:
    0
    Views:
    464
    Tom Brown
    Sep 22, 2005
  2. Mark Probert

    net::telnet and broken pipe

    Mark Probert, Jun 1, 2005, in forum: Ruby
    Replies:
    0
    Views:
    332
    Mark Probert
    Jun 1, 2005
  3. mj
    Replies:
    0
    Views:
    139
  4. Florian Weingarten

    IO.popen(), Timeout, broken pipe

    Florian Weingarten, Dec 17, 2009, in forum: Ruby
    Replies:
    0
    Views:
    308
    Florian Weingarten
    Dec 17, 2009
  5. malgosia askanas

    Broken pipe problem? If so, why?

    malgosia askanas, Mar 28, 2005, in forum: Perl Misc
    Replies:
    2
    Views:
    217
Loading...

Share This Page