本文簡要介紹ruby語言中 Fiber.schedule
的用法。
用法
schedule { |*args| ... } → fiber
該方法是 expected
以立即在單獨的非阻塞光纖中運行提供的代碼塊。
puts "Go to sleep!"
Fiber.set_scheduler(MyScheduler.new)
Fiber.schedule do
puts "Going to sleep"
sleep(1)
puts "I slept well"
end
puts "Wakey-wakey, sleepyhead"
假設 MyScheduler 被正確實現,這個程序將產生:
Go to sleep! Going to sleep Wakey-wakey, sleepyhead ...1 sec pause here... I slept well
…例如:在 Fiber
( sleep(1)
) 內的第一個阻塞操作中,控製權交給了外部代碼(主纖程)和 at the end of that execution
,調度程序負責正確恢複所有阻塞的纖程。
請注意,上麵說明的行為是expected
方法的行為方式,實際行為取決於當前調度程序對 Fiber::SchedulerInterface#fiber
方法的實現。 Ruby 不強製此方法以任何特定方式運行。
如果未設置調度程序,則該方法引發 RuntimeError (No scheduler is available!)
。
相關用法
- Ruby Fiber.scheduler用法及代碼示例
- Ruby Fiber.transfer用法及代碼示例
- Ruby Fiber.new用法及代碼示例
- Ruby Fiber.backtrace_locations用法及代碼示例
- Ruby Fiber.backtrace用法及代碼示例
- Ruby Fiber類用法及代碼示例
- Ruby FiberError類用法及代碼示例
- Ruby File.identical?用法及代碼示例
- Ruby FileUtils.mkdir用法及代碼示例
- Ruby FileUtils.compare_file用法及代碼示例
- Ruby FileUtils.options_of用法及代碼示例
- Ruby File.dirname用法及代碼示例
- Ruby FileUtils.ln_s用法及代碼示例
- Ruby Fiddle.dlwrap用法及代碼示例
- Ruby File.directory?用法及代碼示例
- Ruby FileUtils.install用法及代碼示例
- Ruby File.link用法及代碼示例
- Ruby FileUtils.chown_R用法及代碼示例
- Ruby File.expand_path用法及代碼示例
- Ruby FileUtils.cp_lr用法及代碼示例
- Ruby File.lstat用法及代碼示例
- Ruby File.umask用法及代碼示例
- Ruby FileUtils.rmdir用法及代碼示例
- Ruby File.absolute_path?用法及代碼示例
- Ruby FileUtils.rm_rf用法及代碼示例
注:本文由純淨天空篩選整理自ruby-lang.org大神的英文原創作品 Fiber.schedule。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。