[ANN] forkoff - parallel processing for ruby enumerables

J

Jeremy Hinegardner

require 'timeout'

begin
Timeout.timeout(seconds){ thread.join }
rescue Timeout::Error
thread.kill rescue nil
end

That'll kill the thread, but not the child process that was forked, at least
that's what I remember.

Try this as a general strategy:

parent_t = Thread.new(cmd) do |exec_me|
if cpid = fork then
Thread.current[:cpid] = cpid
cpid, exit_status = Process.waitpid2(cpid)
Thread.current[:exit_status] = exit_status
else
exec exec_me
end
end


unless parent_t.join( seconds ) # seconds contains your timeout value
cpid = parent_t[:cpid]
%w[ TERM KILL ].each do |sig|
Process.kill(sig, cpid)
break if parent_t.join(1)
end

# Do something with Thread.current[:exit_status] to report the child
# process exit status.

enjoy,

-jeremy
 

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

Similar Threads

[ANN] forkoff-0.0.4 0
[ANN] forkoff-0.0.1 0
[ANN] forkoff-1.1.0 0
[ANN] terminator-0.4.2 0
[ANN] Terminator 0.4.4 0
[ANN] slave-1.2.1 1
[ANN] slave-1.1.0 11
[ANN] state-0.4.2 0

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,159
Latest member
SweetCalmCBDGummies
Top