gpt4 book ai didi

events - 协程 vs 事件驱动编程

转载 作者:行者123 更新时间:2023-12-04 12:24:44 24 4
gpt4 key购买 nike

关于维基百科中的例子:
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

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

最佳答案

我认为协程是“传统的”,而事件是“现代的”。然而,它们也有不同的用途; AFAIK,协程可以指定在哪里转移控制(如方法调用)或用于 time-share ,而事件是松散耦合的通信(即在 layered architecture 中“向上”通信)。

请务必阅读Eric Lippert's blog series (从 2010 年 10 月开始)关于连续传球风格,如果你对这些感兴趣的话。有一篇名为“Musings about coroutines”的帖子。

关于events - 协程 vs 事件驱动编程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4983560/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com