Object
A helper class that allows you to run a block inside of a fork, and then get the result from that block.
forker = Spork::Forker.new do
sleep 3
"success"
end
forker.result # => "success"
# File lib/spork/forker.rb, line 15
15: def initialize(&block)
16: return unless block_given?
17: @child_io, @server_io = UNIXSocket.socketpair
18: @child_pid = Kernel.fork do
19: begin
20: @server_io.close
21: Marshal.dump(yield, @child_io)
22: # wait for the parent to acknowledge receipt of the result.
23: master_response = Marshal.load(@child_io)
24: rescue EOFError
25: nil
26: rescue Exception => e
27: puts "Exception encountered: #{e.inspect}\nbacktrace:\n#{e.backtrace * %(\n)}"
28: end
29:
30: # terminate, skipping any at_exit blocks.
31: exit!(0)
32: end
33: @child_io.close
34: end
abort the current running fork
# File lib/spork/forker.rb, line 56
56: def abort
57: if running?
58: Process.kill(Signal.list['TERM'], @child_pid)
59: @child_pid = nil
60: true
61: end
62: end
Wait for the fork to finish running, and then return its return value.
If the fork was aborted, then result returns nil.
# File lib/spork/forker.rb, line 39
39: def result
40: return unless running?
41: result_thread = Thread.new do
42: begin
43: @result = Marshal.load(@server_io)
44: Marshal.dump('ACK', @server_io)
45: rescue ForkDiedException, EOFError
46: @result = nil
47: end
48: end
49: Process.wait(@child_pid)
50: result_thread.raise(ForkDiedException) if @result.nil?
51: @child_pid = nil
52: @result
53: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.