开发者

Stopping a Distributed Ruby Service

I have a script that starts up a DRb service, before spawning a handler object and waiting via DRb.thread.join. I would like the script to run until explicitly killed, so I added

t开发者_StackOverflowrap "INT" do
  DRb.stop_service
end

which successfully stops the DRb service and exits under Ruby 1.8, but under 1.9 seems to deadlock (on OS X 10.6.7). Sampling the process shows a couple of threads spinning in semaphore_wait_signal_trap.

I assume that I'm doing something wrong in the way I'm calling stop_service, but I'm not sure what. Could anyone give me any pointers around how to correctly go about it?


Okay, I think I have found the solution. If I replace the original code with

begin
  DRb.thread.join
rescue Interrupt
ensure
  DRb.stop_service
end

The Ctrl-C works and stops the service.


DRb.thread.join makes the calling thread wait for DRb execution thread to end. If you want to catch INT signal I'd rather go with the following code instead.

$execute = true
DRb.start_service

Signal.trap("INT") { $execute = false }
while $execute
  sleep 1
end
DRb.stop_service

Note that there is no DRb.thread.join in this case. Also catching the signal is the preferred way instead of rescuing the Interrupt exception.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜