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倍左右(没达到一个数量级,有点意外)。
分享到:
相关推荐
information about applying the Ruby platform to create dynamic technology solutions. The series is based on the premise that the need for expert reference books, written by experienced practitioners, ...
Ruby Way by Hal fulton its about Ruby language programming
Here is some information about the versions of softwares I'm using OS: Windows XP Browser: Firefox 3.6.8 Ruby: 1.8.7 Gems: 1.3.7 Watir: 1.6.5 FireWatir: 1.6.5 I would really appreciate all the help....
A quick tutorial about Ruby on Rails
including a description of all the standard library modules, a complete reference to all built-in classes and modules (including all the new and changed methods introduced by Ruby 1.9, 1.9.2, 1.9.3, ...
and you’ve seen bookstore shelves overflowing with programming titles. What is unusual is that Ruby is a very advanced programming language, yet this book claims to teach it to you while ...
On the other hand, Beginning Ruby on Rails is all about programming, so if you have programming expe- rience, that will be helpful. If you want to know how to create Ruby on Rails online applications,...
關於speech recognition code 利用matlab編程
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...
锥形单光纤光镊的多次捕获研究,刘志海,梁佩博,我们对锥形光纤尖的多捕获特性进行了研究。通过使用时域有限差分(FDTD)法对这种锥形光纤尖的捕获力特性进行仿真,结果表明被捕获粒
公案按文件划分为多个区域,散列包含在about_hashes.rb ,模块在about_modules.rb中引入,等等。 它们在path_to_enlightenment.rb文件中按顺序显示。 每个公案都建立了您对 Ruby 的了解并建立在其自身之上。 它会...
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)
AboutTime – 古老但实用的网络校时软件
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)是屬於概念性介紹,對想要入門的人來講不錯