RCR: IO.popen4

R

Roger Pack

Currently with the std lib there is no (built in) way to get the PID of
a currently running child process (short of using fork, which is
unavailable on windows).

i.e.

IO.popen("ls") # what's the PID?
r,w,e,thread = Open3.popen3("ls") # what's the PID?



begin
Timeout.timeout(1) {
system("bash /c sleep")
}
rescue Timeout::Error
# what's the pid, so I can kill it?
end

Jruby has come up with a new method,

pid,r,e,w = IO.popen4("ls")

suggestion: this should be a part of std lib, for the reasons I
discussed above.
Any thoughts on this one?
Thanks.
-rp
 
R

Roger Pack

Tanaka said:
Use IO#pid.


Use thread.pid.

Oh cool thanks.
On 1.8 there appears to be no thread...

Open3.popen3("ls")
=> [#<IO:0xf7323c30>, #<IO:0xf7323bf4>, #<IO:0xf7323b90>]

Any option there?

Also wouldn't it be good to have a popen4 method so you can avoid
starting an extra thread (though I suppose that Process.spawn already
gives us that functionality...)

-r
 
C

Charles Oliver Nutter

Jruby has come up with a new method,

pid,r,e,w = IO.popen4("ls")

suggestion: this should be a part of std lib, for the reasons I
discussed above.

We added both open3 and open4 because the existing implementations
used fork. open3 is just all three streams and open4 also provides the
pid. open3 is of course normally from require 'open3', which in JRuby
now just uses the builtin open3. open4 is equivalent to the open4 gem,
which also uses fork and can't work on JRuby.

- Charlie
 
C

Charles Oliver Nutter

You might already be aware of the fact, that 1.9's open3 no longer uses fork.

I did not, but this may help. I believe both spawn and detach should
be possible in JRuby, and the JDK's process-wrangling APIs work in a
similar way already.

We'll still need a custom version for 1.8 mode though.

- Charlie
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,815
Latest member
treekmostly22

Latest Threads

Top