Coroutine与事件驱动编程

How*_*ard 7 events event-driven-design coroutine

关于维基百科中的示例:http: //en.wikipedia.org/wiki/Coroutine

var q := new queue

coroutine produce
    loop
        while q is not full
            create some new items
            add the items to q
        yield to consume

coroutine consume
    loop
        while q is not empty
            remove some items from q
            use the items
        yield
Run Code Online (Sandbox Code Playgroud)

我只是想知道基于传统事件的方法可以处理这种使用模式,为什么需要使用协同程序?

Mar*_*son 5

我认为协程是“传统的”,事件是“现代的”。但是,它们也有不同的目的。AFAIK,协程可以指定将控制权转移到何处(如方法调用),也可以用于分时,而事件是松散耦合的通信(即在分层体系结构中 “向上”通信)。

如果您对类似这样的事情感兴趣,请务必阅读Eric Lippert的博客系列(自2010年10月起),有关延续传递风格。有一个标题为“ 关于协程的缪斯 ”的帖子。