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----
分享到:
相关推荐
Ruby-LightIO是一个针对Ruby开发者的网络库,其核心特性在于将Ruby的Fiber和高效的IO事件循环(event loop)相结合。这个库的设计目标是为了提供更高效、更轻量级的网络服务处理能力,尤其适合于构建高并发、低延迟...
4. **轻量级多任务**:介绍如何使用Fiber和Thread来实现并发编程,提高程序的执行效率。 5. **异常处理进阶**:进一步探讨异常处理的高级用法,如自定义异常类、异常传递链等,帮助开发者写出更加健壮的应用程序。 ...
- Ruby提供了Thread、Fiber和Concurrent Ruby库等工具进行并发处理。理解线程安全和GIL(全局解释器锁)限制是提高多线程程序效率的关键。 通过阅读"Ruby Trap"电子书,初学者可以对这些潜在的问题有更深入的理解...
3. ** Fiber和Concurrent Programming**:Ruby 2.0引入了Fiber,这是一种轻量级线程,用于实现协程。这为开发者提供了更好的并发处理能力,尤其是在I/O密集型任务中。 4. **块参数**:Ruby 2.0允许块参数以`&`符号...
- 使用`Thread`和`Fiber`进行轻量级并发,或者使用JRuby(Java平台的Ruby实现)实现真正的多线程。 通过以上知识点,开发者可以有效地测试和优化Ruby应用,确保其在各种工作负载下都能表现出良好的性能。记住,...
1. **高性能**:Sneakers利用Ruby的 Fiber 和 Concurrent Ruby 库来实现轻量级并发,确保高吞吐量和低延迟。 2. **工作进程**:Sneakers支持多工作进程模型,每个进程可以处理多个并发任务,进一步提高处理能力。 3....
5. **GIL(全局解释器锁)与并发**:尽管Ruby有GIL限制了多线程的并行执行,但它依然支持高效的并发处理,比如使用Fiber或者线程池来实现并发。 6. **丰富的库和框架**:Ruby拥有众多高质量的库,如Ruby on Rails...
Ruby的Thread和 Fiber库支持并发编程,尽管GIL(全局解释器锁)限制了多核处理器下的并行性,但在处理I/O密集型任务时仍能发挥优势。 通过"Ruby.pdf"和"ruby tutorial.pdf"这两份文档,你可以系统地学习和掌握以上...
11. **异步编程**:Ruby 2.6引入了 Fiber 和 Concurrent Ruby 库,使得异步编程变得更加简单,可以实现非阻塞I/O,提高应用性能。 12. **Web框架**:Ruby on Rails是最著名的Web开发框架,它遵循MVC(模型-视图-...
并发和异步编程方面,Ruby支持线程和并发处理,并提供了Fiber和协程的概念,这对于处理高并发应用至关重要。异步编程库,例如EventMachine和Celluloid,是处理I/O密集型任务的利器。 在Web开发方面,Ruby on Rails...
结合标签"ruby",我们可以进一步探讨Ruby中的并发编程概念,如 Fiber 和 Concurrent Ruby 库。Fiber是轻量级线程,它允许你在单一线程内实现协程,提高代码的可读性和性能。而Concurrent Ruby库提供了多种并发模型,...
- ** Fibers and Concurrency**:Ruby 2.0 引入了 Fiber,这是一种轻量级线程,允许更好的并发处理,尽管它们并不等同于操作系统级别的线程。 - **Stackless Ruby**:虽然 Ruby 2.0 不是完全的 Stackless Ruby,但...
- Fiber:用于实现协程的结构,允许用户控制线程的切换。 学习Ruby语言,不仅需要理解这些基础知识,还需要通过实践编写代码,参与开源项目,以及阅读其他开发者的代码,不断提升自己的编程技能和解决问题的能力。...
5. **并发编程**:Ruby的Thread和Fiber可以实现多线程和协程,案例可能涉及并发和同步问题的解决。 通过这个压缩包,学习者不仅可以了解Ruby的基本语法,还能通过实际案例加深对面向对象编程的理解,掌握Ruby在不同...
专为Ruby 3.0 Fiber Scheduler设计的事件库。 此gem仍在开发中,API和功能不稳定。咨询和公关很受欢迎。 特征 IO后端支持 Linux 视窗 苹果系统 FreeBSD的 io_uring :warning: (参见1) :cross_mark: :cross_...
- **Ruby 2.0** 引入了垃圾收集(GC)的改进,增加了一对多线程支持,以及对 Fiber 的改进,用于实现轻量级并发。 - **Ruby 2.1** 开始,性能持续提升,引入了更高效的内存管理,并添加了方法引用和惰性枚举。 - **...
- Fiber是轻量级的协程,可以实现用户级别的并发。 通过阅读《Ruby编程语言》和《Programming Ruby》等经典书籍,结合实践项目,你可以深入理解并掌握Ruby的精髓。在学习过程中,不断地编写代码、解决问题和参与...
5. **并发与多线程**:Ruby 支持多种并发模型,包括 Fiber、Thread 和 Process。源码中可能涉及如何在 Ruby 中有效地实现并发,避免常见的并发问题。 6. **错误调试与日志**:有效的错误处理和日志记录是任何项目的...
然而,通过合理的设计和使用外部库(如多进程、Fiber等),仍然可以在Ruby中实现高性能的并发程序。 #### 四、示例代码分析 以下是一个简单的Ruby多线程程序示例,演示了如何创建和管理两个并发执行的线程: ```...
10. **异步编程与并发**:Ruby通过`Thread`类支持多线程,`fiber`则提供轻量级的协程。`EventMachine`是一个事件驱动的网络库,用于处理并发连接。 通过学习和分析“Ruby Source Code”中的示例,你将能够掌握Ruby...