coroutine and yield
来源:互联网 发布:linux expect root 编辑:程序博客网 时间:2024/04/29 07:39
Inside a function, the yield statement can also be used as an expression that appears on the right side of an assignment operator.
A function that uses yield in this manner is known as a coroutine, and it executes in response to values being sent to it.
For example:
def receiver():
print("Ready to receive")
while True:
n = (yield)
print("Got %s" % n)
>>> r = receiver()
>>> r.next() # Advance to first yield (r._ _next_ _() in Python 3)
Ready to receive
>>> r.send(1)
Got 1
>>> r.send(2)
Got 2
>>> r.send("Hello")
Got Hello
>>>
In this example, the initial call to next() is necessary so that the coroutine executes statements leading to the first yield expression.
A coroutine will typically run indefinitely unless it is explicitly shut down or it exits on its own.To close the stream of input values, use the close() method like this:
>>> r.close()
>>> r.send(4)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
StopIteration
- coroutine and yield
- coroutine yield 多线程
- Unity3D中协程Coroutine&yield
- Python yield coroutine
- Unity3D中的Coroutine 与 Yield
- python generator yield 协程 coroutine
- 用C#中的yield实现Coroutine框架
- Unity 3D Coroutine&yield(C#脚本)
- 说说Python中的iterator,yield表达式及generator,coroutine
- 简单总结协程Coroutine及Yield常见用法
- 关于线程Thread、协程Coroutine、生成器Generator、yield资料
- 简单总结协程Coroutine及Yield常见用法
- unity3d Unity协程(Coroutine)yield 表达式解释
- generator and yield
- Generator and yield
- Python yield and generator
- Python yield and iterables
- yield and Generators
- java解析xml的方式总结
- Real6410移植linux-2.6.39.1记录(3)-USB驱动移植(USB HOST以及USB OTG)
- [deep learning] 部分论文
- NYOJ 551 移动小球
- Gamification:使用游戏策略影响用户行为的入门介绍(1)
- coroutine and yield
- testdbg-测试调试器
- Fast & Furious face detection with OpenCV
- dataset遍历所需要的列
- inputStream转换为String和保存到文件
- Struts2中的ModelDriven机制及其运用
- android系统之sensor学习
- servlet.jar--jar not loaded错误
- redmine上传文件报错问题解决