我试图了解以下代码片段的行为。我的重点是Fiber#transfer
方法。
require 'fiber'
fiber2 = nil
fiber1 = Fiber.new do
puts "In Fiber 1" # 3
fiber2.transfer # 4
end
fiber2 = Fiber.new do
puts "In Fiber 2" # 1
fiber1.transfer # 2
puts "In Fiber 2 again" # 5
Fiber.yield # 6
puts "Fiber 2 resumed" # 10
end
fiber3 = Fiber.new do
puts "In Fiber 3" # 8
end
fiber2.resume # 0
fiber3.resume # 7
fiber2.resume # 9
我已经用右侧的预期执行顺序对代码行进行了编号。一旦fiber3.resume
返回并调用,我希望在标记为#10fiber2.resume
的行内继续执行。相反,我收到以下错误:fiber2
fiber2.rb:24:in `resume': cannot resume transferred Fiber (FiberError)
from fiber2.rb:24:in `<main>'
这是清单最后一行报告的错误:fiber2.resume
.