python coroutine_python coroutine
python coroutine
前言
OOAD课程是用python教的,其中涉及到大量的python语法糖,老师也算是手把手教如何写出OO的python,但一个学期没听课只能期末补一补。
Coroutines
Coroutines are similar to generators with a few differences. The main differences are:
generators are data producers generators制造数据
coroutines are data consumers coroutines消费数据
来看第一个代码示例,通过send()接收数据,然后进行比对,找出含有关键字的数据并打印。
def grep(pattern):
print("Searching for", pattern)
while True:
line = (yield)
if pattern in line:
print(line)
search = grep('coroutine')
next(search)
# Output: Searching for coroutine
search.send("I love you")
search.send("Don't you love me?")
search.send("I love coroutines instead!")
很明显我们可以看到,yield关键字在这里已经从返回数据的变为接收数据的。也就是,Python的yield不但可以返回一个值,它还可以接收调用者发出的参数。
在中文里,coroutine对应的是协程,但是显然老师的重点不是什么同步异步,而是yield,以及coroutine和subprogram的区别。
Subprogram vs Coroutine, is (programming) a piece of code that performs a task, and that can be passed new input and return output more than once. As nouns the difference between subroutine and coroutine is that subroutine is (computer science) a section of code, called by the main body of a program, that implements a task while coroutine is (programming) a piece of code that performs a task, and that can be passed new input and return output more than once.
放上老师PPT的测试代码就收工:
def coroutine(y):
for i in range(y):
x = yield i
print("i=%d , x=%d " % (i, x))
c = coroutine(4)
next(c)
c.send(10)
c.send(20)
c.send(30)
def echo():
just_received='nothing'
try:
while True:
received=yield just_received
just_received=received
print('I got {}.'.format(just_received))
except GeneratorExit:
# when closed with close()
print('Coroutine closed!')
g = echo()
next(g)
g.send('test1')
g.send('test2')
g.close()
Reference
python coroutine_python coroutine相关推荐
- python coroutine_Python coroutine的坑
目前有个后端worker用的python 2.7写的,一次起10个gen.coroutine来跑黑盒的代码,这些代码很多是在做HTTP request. 在server端发现这些来自worker的la ...
- python coroutine_python coroutine的学习跟总结[转]
简介 因为最近一段时间需要研究一些openstack相关的东西,在阅读一些相关代码的时候碰到很多python特定的一些特性,比如generator, coroutine以及一些相关的类库,比如even ...
- python coroutine_python coroutine的学习和总结
简介 因为最近一段时间需要研究一些openstack相关的东西,在阅读一些相关代码的时候碰到很多python特定的一些特性,比如generator, coroutine以及一些相关的类库,比如even ...
- PYTHON 之 COROUTINE
参考资料 资料 https://blog.csdn.net/andybegin/article/details/77884645 http://python.jobbole.com/86481/ ht ...
- just函数 python_在python里写Monad
这段时间写微信小程序看见callback hell非常炸毛,而且确实我不熟悉js的各种衍生工具(async/await, promise),想着python的coroutine是yield模改出来的, ...
- python的前端框架_web前端三大主流框架之Python异步框架如何工作?
这篇文章我们从 socket 编程的例子来看看 Python 异步框架是如何工作的,需要了解下简单的 socket 编程以及 Linux 提供的 I/O 复用机制. Python 异步框架也是基于操作 ...
- python中data.find_all爬取网站为空列表_利用Golang快速爬取盗版网站的整套音频
01 前言 最近因为 Zigma 帮我写了个推广 Catcher 小程序软文的原因,答应了他帮他爬了一个盗版音频网站的整套 <李淼谈奇案> . 在制作爬虫脚本的过程中,也是遇到了一些有趣的 ...
- 从无栈协程到C++异步框架
导语 在前面的文章中我们尝试从 C++17 和 C++20 的角度分别探讨过其中无栈协程的包装机制和使用, 但其中的设计由来, 原理, 剥析的并不多. 这也导致对相关特性不太熟悉的读者要理解相关内容存 ...
- 从无栈协程到 C++异步框架
作者:fangshen,腾讯 IEG 游戏客户端开发工程师 导语 本文我们将尝试对整个 C++的协程做深入浅出的剥析, 方便大家的理解. 再结合上层的封装, 最终给出一个 C++异步框架实际业务使用的 ...
最新文章
- 三角形周长最短问题_谈“最短”
- python字符串格式化方法 format函数的使用
- 平衡二叉树AVL删除
- jquery选择器连续选择_JQuery中的选择器
- 第 190 场力扣周赛
- 谷歌Pixel 6系列手机发布会官宣定档 10月19日发布
- 问题-Delphi7中JSON遍历节点不支持使用IN处理方法?
- 2020年最新的PHP面试题(附答案)
- Synchronous Bidirectional Inference for Neural Sequence Generation
- dp规划之矩阵连乘问题
- xampp使用教程php,php集成套件服务器xampp安装使用教程
- 合成器与电子音乐词语解释-比较齐全版
- 智慧园区SaaS管理系统解决方案:赋能园区实现信息化、数字化管理
- oracle优化方法,九大Oracle性能优化基本方法详解
- eclipse的优缺点
- ARM 电源管理 4种模式
- 计算机显示器的视频接口有什么不同,电脑显示器用VGA还是HDMI好有什么区别
- 电脑双核CPU具体是什么意思?
- Linux 命令随笔
- R语言主成分分析总结