Forkoff - parallel processing for ruby enumerables

Abdul-rahman Advany wrote:

Checking out the source I only see pid = fork (this is a call to
Thread.new isn’t it?), I don’t see that real fork (kernel) is used… or
I am wrong?

Sorry, just figured out calling fork makes the thread a child process…
just need to find out how to set a timeout on the thread… anyone
suggestions?

On Apr 29, 2008, at 4:15 PM, Abdul-rahman Advany wrote:

Sorry, just figured out calling fork makes the thread a child
process…
just need to find out how to set a timeout on the thread… anyone
suggestions?

require ‘timeout’

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

a @ http://codeforpeople.com/

On Wed, Apr 30, 2008 at 08:02:36AM +0900, ara.t.howard wrote:

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