这是我编写的一些代码的简化版本:
class InfiniteLoop
def run
trap('SIGTERM') do
puts 'exiting'
exit
end
loop {}
end
end
如果我运行:
InfiniteLoop.new.run
我可以 ctrl+c
并得到:
exiting
但是,当我这样做时:
t = Thread.new { InfiniteLoop.new.run }
sleep 1
t.terminate
我没看到:
exiting
有人能给我指出正确的方向吗?我希望在终止线程时有相同的行为。
最佳答案
如果您没有发送 SIGTERM 信号(通过 ctrl+c),则不会执行陷阱 block 。 另见内核方法:
at_exit { puts 'exiting' }
关于ruby - Thread#terminate 和处理 SIGTERM,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13850554/