From: Jeremy Hinegardner Date: 2008-05-01T00:32:45+09:00 Subject: Re: forkoff - parallel processing for ruby enumerables On Wed, Apr 30, 2008 at 08:02:36AM +0900, ara.t.howard wrote: > > 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 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 -- ======================================================================== Jeremy Hinegardner jeremy@hinegardner.org