本书 Ruby 1.9&2.0编程中给出了以下示例:
require 'irb'
trap "INT" do
IRB.start
end
count = 0
loop do
count += 1
puts count
puts "Value = #{@value}" if defined? @value
sleep 1
end
不幸的是,如果我使用Ruby 2.5.1安装程序运行此代码,则会得到以下ThreadError
( run_irb.rb 是具有上面代码的文件的名称):< / p>
24: from run_irb.rb:8:in `<main>'
23: from run_irb.rb:8:in `loop'
22: from run_irb.rb:12:in `block in <main>'
21: from run_irb.rb:12:in `sleep'
20: from run_irb.rb:4:in `block in <main>'
19: from /usr/lib/ruby/2.5.0/irb.rb:376:in `start'
18: from /usr/lib/ruby/2.5.0/irb/init.rb:17:in `setup'
17: from /usr/lib/ruby/2.5.0/irb/init.rb:112:in `init_config'
16: from /usr/lib/ruby/2.5.0/irb/init.rb:112:in `new'
15: from /usr/lib/ruby/2.5.0/irb/locale.rb:32:in `initialize'
14: from /usr/lib/ruby/2.5.0/irb/locale.rb:108:in `load'
13: from /usr/lib/ruby/2.5.0/irb/locale.rb:124:in `find'
12: from /usr/lib/ruby/2.5.0/irb/locale.rb:145:in `search_file'
11: from /usr/lib/ruby/2.5.0/irb/locale.rb:157:in `each_localized_path'
10: from /usr/lib/ruby/2.5.0/irb/locale.rb:167:in `each_sublocale'
9: from /usr/lib/ruby/2.5.0/irb/locale.rb:158:in `block in each_localized_path'
8: from /usr/lib/ruby/2.5.0/irb/locale.rb:150:in `block in search_file'
7: from /usr/lib/ruby/2.5.0/rubygems.rb:213:in `try_activate'
6: from /usr/lib/ruby/2.5.0/rubygems/specification.rb:1063:in `find_by_path'
5: from /usr/lib/ruby/2.5.0/rubygems/specification.rb:1063:in `find'
4: from /usr/lib/ruby/2.5.0/rubygems/specification.rb:1063:in `each'
3: from /usr/lib/ruby/2.5.0/rubygems/specification.rb:1064:in `block in find_by_path'
2: from /usr/lib/ruby/2.5.0/rubygems/core_ext/kernel_require.rb:40:in `require'
1: from /usr/lib/ruby/2.5.0/monitor.rb:185:in `mon_enter'
/usr/lib/ruby/2.5.0/monitor.rb:185:in `lock': can't be called from trap context (ThreadError)
是否有办法使用Ruby 2.5.1从trap上下文中运行irb?
答案 0 :(得分:2)
Ruby 2.0不允许在信号处理程序中使用Mutex#lock
根据红宝石语言中的bug 7917,这是“预期”行为。因此,您基本上已经找到了语言错误和书籍错误,因为作者没有在ruby 2.0中测试此示例(确认它可以在1.9中运行。)
另一种方法是使用异常/救援,并从这样的中断执行IRB
会话:
require 'irb'
count = 0
loop do
count += 1
puts count
puts "Value = #{@value}" if defined? @value
sleep 1
rescue Interrupt => e
IRB.start
break
end
以上内容将正确捕获中断,并允许IRB
会话开始。我确实注意到IRB.start
不会为您提供本地执行上下文,因此您可能希望查看binding.irb
,以便您启动这样一个更有用的调试会话:
$ ruby thread-test.rb
1
2
^C
[1] irb(main)>
From: /work/_scratch/thread-test/thread-test.rb @ line 11 :
6: puts count
7: puts "Value = #{@value}" if defined? @value
8: sleep 1
9: rescue Interrupt => e
10: binding.irb
=> 11: break
12: end
[2] irb(main)> e
=> Interrupt
[3] irb(main)> count
=> 2
我发现了一些有关处理Signals, Traps, and Rescues和Graceful Shutdown的博客文章,可能会帮助您进一步解决问题。