Ruby 1.9 带来了Fiber: http://www.infoq.com/news/2007/08/ruby-1-9-fibers
Ruby中的Fiber是一种semi-coroutine,第一次看到这个东西挺难理解。
Subroutine VS Coroutine
理解semi-coroutine之前先来了解下什么是coroutine,子程序(subroutine)很容易理解,通过比较能看出它们之间的区别。
Subroutine |
Coroutine |
The lifespan of subroutines is dictated by last in, first out (the last
subroutine called is the first to return)
|
The lifespan of coroutines is dictated entirely by their use and need. |
The start of a subroutine is the only point of entry |
The start of a coroutine is the first point of entry and subsequent
points of entry are following yield commands. |
Subroutines can return only once |
coroutines can return (yield) several times |
Practically, yielding returns the result to the calling coroutine and
gives it back control, like a usual subroutine. However, the next time
the coroutine is called, the execution does not start at the beginning
of the coroutine but just after the yield call.
那什么是semi-coroutine:Semi-Coroutines
are asymmetric Coroutines
which are
limited in their choice of transfer of control. Asymmetric Coroutines
can only transfer control back
to their caller, where Coroutines are free to transfer control to any
other Coroutine, as long as they have a handle to it.
Fiber VS Thread
Ruby的Fiber是一种控制结构(后面会说到Fiber::Core),它的运行方式和操作方法很像thread。
Fiber |
Thread |
not concurrency |
concurrency |
resume, suspend(call Fiber.yield或者等到block结束) |
resume, suspend |
return and give control back to caller |
not return or give control back unless join it
|
Fiber VS Closure
看下面这个方法:
fib = Fiber.new do
f1 = f2 = 1
loop do
Fiber.yield f1 (每次resume,在遇到Fiber.yield或者block结尾时结束)
f1, f2 = f2, f1 + f2 (第二次resume,从这里开始)
end
end
10.times { puts fib.resume }
很容易想到用closure去实现它:
def local_proc
f1 = f2 = 1
return Proc.new {f1, f2 = f2, f1 + f2; p f1;}
end
proc1 = local_proc
10.times {proc1.call}
上面两个方法的实现方式是很不相同的。Fiber方式通过 enable the automatic conversion of internal iterators,
such as each, to enumerator or external iterators来实现。
Fiber::Core VS Fiber
相较于Fiber,Fiber::Core是真正的coroutine -- 一种可以实现userspace thread的轻量级线程。
写道
Next to implementing control structures, Coroutines provide a way to use lightweight concurrency
. In effect, they allow to implement userspace threads
with cooperative scheduling. The Coroutines can either yield control to each other, or have centralized scheduling by handing off control to one scheduler Coroutine which then decides who gets scheduled next.
Ruby1.9实现了kernal threads(sometimes too heavy),所以提供一种轻量级的并行化方式是必要的。
写道
Nevertheless, creating a lot of kernel threads still has a lot of overhead, or might simply cause problems on OSes that have hard thread limits or struggle with large numbers of threads. It's in these cases, when a lightweight alternative is useful. It allows the code to be split among threads, if that is the logic, straightforward solution, but keeps the overhead down.
Fiber for JRuby
实现JRuby等其它ruby实现的Fiber会遇到一些困难:
写道
If Fibers get adopted in Ruby, this will create headaches for Ruby implementations targeting the JVM or CLR, such as JRuby, XRuby, Ruby.NET or IronRuby. None of them currently support Continuations because manipulating or reading the callstack is hard or impossible to do in these VMs
. The lack of Continuations is a controversial issue, but doesn't seem to have caused problems with e.g. JRuby, because they are not widely used in Ruby. The only use of them in the Ruby 1.8 standard library is the Generator implementation, but, for instance, JRuby 1.0, solved this by implementing the same functionality without using Continuations.
即使用workarounds,也有一些值得担心的性能问题:
写道
While it's certainly possible to implement these features using workarounds, the question is whether these workaround will cause performance regressions. If, for instance, call stacks must be emulated on the heap, instead of using the VM's stack, this can lead to lower performance or prevent (JIT) compiler optimizations from being applied.
Workarounds for asymmetric Coroutines would be easier to do, as they could make use of the VM's stack for method invocations.
用途
Coroutines are well-suited for implementing more familiar program
components such as cooperative tasks
,
iterators
,
infinite lists
and pipes
.。
References:
coroutine: http://en.wikipedia.org/wiki/Coroutine
用Ruby的fiber实现pipe的例子:http://pragdave.blogs.pragprog.com/pragdave/2007/12/pipelines-using.html
----EOF----
分享到:
相关推荐
LightIO是一个ruby网络库,它结合了ruby fiber和快速IO event loop
ruby代码ruby代码ruby代码ruby代码ruby代码ruby代码ruby代码ruby代码
ruby DBI ruby DBI ruby DBIruby DBI ruby DBI ruby DBIruby DBI ruby DBI ruby DBIruby DBI ruby DBI ruby DBIruby DBI ruby DBI ruby DBIruby DBI ruby DBI ruby DBIruby DBI ruby DBI ruby DBIruby DBI ruby DBI ...
《Ruby完全自学手册》是一本完全覆盖Ruby和Ruby on Rails的完全自学手册。《Ruby完全自学手册》的特色是由浅入深、循序渐进,注重理论和实践的结合。虽然定位为入门手册,但是依然涉及许多高级技术和应用,覆盖到的...
ruby2ruby 提供一些用来根据 RubyParser 兼容的 Sexps 轻松生成纯 Ruby 代码的方法。可在 Ruby 中轻松实现动态语言处理。 标签:ruby2ruby
ruby源代码 ruby源代码 ruby源代码 ruby源代码2
Ruby编程,Ruby编程,Ruby编程,Ruby编程,Ruby编程,
ruby源代码 ruby源代码 ruby源代码 ruby源代码4
ruby笔记1ruby笔记1ruby笔记1ruby笔记1ruby笔记1ruby笔记1
ruby-build - 编译和安装Ruby
ruby源代码 ruby源代码 ruby源代码 ruby源代码3
ruby打包文件ruby打包文件ruby打包文件ruby打包文件ruby打包文件ruby打包文件ruby打包文件ruby打包文件ruby打包文件
ruby interpreter 原理探討 At first glance, learning how to use Ruby can seem fairly simple. Developers around the world find Ruby’s syntax to be graceful and straightforward. You can express ...
内含以下4个文档: 1、Addison.Wesley.The.Ruby.Way.2nd.Edition.Oct.2006.chm 2、O'Reilly.Learning.Ruby.May.2007.chm 3、Programming Ruby 2e.pdf 4、ruby中文文档.chm
ruby-4-variables.mp4 ruby-5-variables-rules.mp4 ruby-6-expression.mp4
Ruby,一种简单快捷的面向对象(面向对象程序设计)脚本语言,在20世纪90年代由日本人松本行弘(Yukihiro Matsumoto)开发,遵守GPL协议和Ruby License。它的灵感与特性来自于 Perl、Smalltalk、Eiffel、Ada以及 Lisp ...
Ruby开发流程涉及多个关键步骤,从环境搭建到项目部署,下面是一个大致的流程概述: Ruby开发环境搭建:首先,需要在计算机上安装Ruby开发环境。推荐使用macOS或各种发行版本的Linux作为运行环境,尽管Windows下也...
ruby-git, ruby/Git是一个 ruby 库,可以通过将系统调用包装到Git二进制文件来创建读取和操作Git存储库 用于 ruby的 Git库在 ruby 中使用Git的库。主页项目源代码的Git public 位于:...
Ruby on Rails Guides v2 - Ruby on Rails 4.2.5
内含 12 本 Ruby 语言中英文教程资源,本资源下载后解压缩将得到以下图书: Programming Ruby 2nd.pdf Agile Web Development with Rails 2nd ed.pdf Agile Web Development with Rails.pdf Best.of.Ruby.Quiz.pdf O...