ruby 1.9 引入了fiber,fiber的接口调用主要为:
1. Fiber.yield / fiber.resume
yield用于中断fiber执行,从启动该fiber的代码后继续执行。一个fiber被初始化之后,必须要通过resume方法启动才能执行fiber中的代码,执行时遇到Fiber.yield就把控制权交回到resume调用方。接下来再调用fiber.resume就会把控制权交会给fiber,从yield之后继续运行。
两个方法都可以带参数, yield后的参数,会在控制权移交给resume后传递给resume,作为fiber.resume这个方法调用的返回值;同样的,resume后的参数也会作为Fiber.yield的返回值,这样就可以进行通信了。此外,如果resume是第一次调用,参数会传给Fiber.new do|block|...end中的block
2. Fiber.current
获取当前的fiber引用
3. fiber.alive?
fiber运行结束后,再次调用resume会出错,这个方法用来查询是否还健在
利用fiber执行任务队列:
通过yield和resume传参来实现
fiber = Fiber.new do|block|
while true
block.call
block = Fiber.yield
end
end
while !task_queue.empty?
fiber.resume(task_queue.poll)
end
不过,在2g的机器上,fiber调度的速度大约是100w/次的调度,不过不确定cpu有没有因为turbo boost提高到3g。这个速度大概是thread调度的3倍左右(没达到一个数量级,有点意外)。
分享到:
相关推荐
and the description "Ruby.Pocket.Reference.Jul.2007 learning ruby 的 can reference" indicate that this is a concise guide designed to provide quick access to essential information about the Ruby ...
《Ruby Way》是由Hal Fulton编写的关于Ruby编程语言的一本著作。这本书深入浅出地探讨了Ruby语言的各种特性,旨在帮助读者理解并掌握这门强大的动态脚本语言。Ruby以其简洁、优雅的语法和强大的元编程能力而备受赞誉...
在"ruby-language-quickref.zip_About Language"这个压缩包中,我们重点关注的是Ruby语言本身的基础知识和特性。"ruby language quickref.pdf"很可能是一个Ruby语言速查指南,涵盖了语言的关键概念和语法。 首先,...
关于AboutTime时间校对工具,这是一款专用于网络时间同步的软件,它的主要功能是帮助用户确保计算机系统的时间准确无误。在计算机领域,时间的准确性对于许多应用和服务至关重要,例如金融交易、日志记录、分布式...
About Time AND Timer sample in STEP7_20070903a.zip西门子PLC编程实例程序源码About Time AND Timer sample in STEP7_20070903a.zip西门子PLC编程实例程序源码About Time AND Timer sample in STEP7_20070903a.zip...
公案按文件划分为多个区域,散列包含在about_hashes.rb ,模块在about_modules.rb中引入,等等。 它们在path_to_enlightenment.rb文件中按顺序显示。 每个公案都建立了您对 Ruby 的了解并建立在其自身之上。 它会...
- **第6章:More About Methods**:详解方法(method)的定义与调用机制。 - **第7章:Expressions**:讨论表达式在Ruby中的应用。 #### 四、进阶技术 - **第8章:Exceptions, Catch, and Throw**:异常处理是任何...
With WS2-based SAs, dark solitons in erbium-doped fiber (EDF) lasers are first obtained. For the generated dark solitons, the center wavelength is measured to be 1530 nm, and the repetition rateis ...
like how to blink the lights on your keyboard. <br> <br> The Ruby Cookbook is the most useful book yet written about Ruby. When you need to solve a problem, don't reinvent the wheel: ...
[奥莱理] Exploring Everyday Things with R and Ruby Learning About Everyday Things (E-Book) ☆ 出版信息:☆ [作者信息] Sau Sheong Chang [出版机构] 奥莱理 [出版日期] 2012年07月30日 [图书页数] 252页...
This paper presents the construction of a Knowledge Graph about relations between agents in a political system. It discusses the main modeling challenges, with emphasis on the issue of trust and ...
About Oracle Scripting in Interaction Center Family 11.5.10 (Doc ID 270443.1)
about to fork child process, waiting until server is ready for connections. forked process: 3560 ERROR: child process failed, exited with error number 1 To see additional information in this output, ...
This paper synthesises and extends emerging critical thinking about algorithms and considers how best to research them in practice. Four main arguments are developed. First, there is a pressing need ...
有15頁介紹Standardization activities about functional safety in automotive (AUTOSAR, ISO 26262, CESAR)是屬於概念性介紹,對想要入門的人來講不錯
Create an awesome About Page for your Android App in 2 minutes This library allows to generate beautiful About Pages with less effort, it's fully customizable and supports opening specific intent ...