什么是协程? [英] What is a coroutine?

查看:110
本文介绍了什么是协程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是协程?它们与并发有何关系?

What is a coroutine? How are they related to concurrency?

推荐答案

协程与并发在很大程度上是正交的。协程是一种通用的控制结构,流控制在两个不同的例程之间协同传递而不返回。

Coroutines and concurrency are largely orthogonal. Coroutines are a general control structure whereby flow control is cooperatively passed between two different routines without returning.

Python中的 yield语句就是一个很好的例子。它创建一个协程。遇到 yield时,将保存该函数的当前状态,并将控制权返回给调用函数。然后,调用函数可以将执行转移回给yield函数,并且其状态将恢复到遇到 yield的位置,并且执行将继续。

The 'yield' statement in Python is a good example. It creates a coroutine. When the 'yield ' is encountered the current state of the function is saved and control is returned to the calling function. The calling function can then transfer execution back to the yielding function and its state will be restored to the point where the 'yield' was encountered and execution will continue.

这篇关于什么是协程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆