I had occasion to install the win32-process gem today, because my Watir tests needed to start a background agent. On my laptop the cookbook example in the gem’s documentation doesn’t work; I found:
- The package to require is ‘win32/process’;
- the block version of
fork()
causes the child process to execute the block and then continue with the parent code; - the module names in the examples are incorrect (and not needed); and
- the return value from
Process.create()
is not a process ID, but a structure that contains a process ID.
Nothing major, but it wasted twenty minutes. Anyway, for completeness here are a couple of corrected example scripts showing the behaviour of win32-process
. First, mimicking the old Unix API:
require "win32/process" pid = Process.fork if pid.nil? 3.times { puts "In the child" sleep 1 } exit end 2.times { puts "In the parent" sleep 1 } Process.wait
As expected, this produces
In the parent In the child In the parent In the child In the child
Now try passing a block to the child:
require "win32/process" Process.fork do 3.times { puts "In the child" sleep 1 } exit end 2.times { puts "In the parent" sleep 1 } Process.wait
On my PC this produces:
In the parent In the child In the parent In the child In the child In the parent In the parent
Those extra prints from the parent show that the child process continued after completing the block!
Yep, the examples on RF are old and need fixing. I’ll update those asap and see if I can remember what’s happening with Process.fork’s block method.
Regards,
Dan