python协程用法实例分析
作者:MaxOmnis 时间:2021-09-03 15:48:01
本文实例讲述了python协程用法。分享给大家供大家参考。具体如下:
把函数编写为一个任务,从而能处理发送给他的一系列输入,这种函数称为协程
def print_matchs(matchtext):
print "looking for",matchtext
while True:
line = (yield)
#用 yield语句并以表达式(yield)的形式创建协程
if matchtext in line:
print line
>>> matcher = print_matchs('python')
>>> matcher.next()
looking for python
>>> matcher.send('hello python')#看生成器那片,关于send()跟next()的区别
hello python
>>> matcher.send('test')
>>> matcher.send('python is cool')
python is cool
>>>matcher.close()
希望本文所述对大家的Python程序设计有所帮助。
标签:python,协程
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
python采用django框架实现支付宝即时到帐接口
2023-07-16 00:15:33
如何操作Access数据库?
2009-11-11 19:23:00
pandas object格式转float64格式的方法
2022-05-26 15:58:53
python的多重继承的理解
2021-04-27 16:54:21
![](https://img.aspxhome.com/file/2023/7/101737_0s.jpg)
画好线框图的20个步骤
2009-09-01 19:46:00
css中如何使div居中(垂直水平居中)
2007-08-13 08:17:00
在ASP.NET 2.0中操作数据之五十:为GridView控件添加Checkbox
2023-06-26 19:18:09
![](https://img.aspxhome.com/file/2023/1/75531_0s.gif)
Python数据可视化图实现过程详解
2022-08-30 19:24:17
![](https://img.aspxhome.com/file/2023/7/81557_0s.jpg)
python数据可视化JupyterLab实用扩展程序Mito
2021-01-24 13:42:04
![](https://img.aspxhome.com/file/2023/9/83769_0s.jpg)
Python pip安装lxml出错的问题解决办法
2021-11-17 07:36:07
Python关于__name__属性的含义和作用详解
2021-10-28 09:29:51
![](https://img.aspxhome.com/file/2023/2/77592_0s.png)
python命名关键字参数的作用详解
2023-09-01 10:35:37
Python面向对象之反射/自省机制实例分析
2022-07-27 06:57:53
使用python实现微信小程序自动签到功能
2021-05-30 10:04:11
![](https://img.aspxhome.com/file/2023/0/64660_0s.jpg)
Oracle Session每日统计功能实现
2023-07-22 09:47:24
![](https://img.aspxhome.com/file/2023/7/64087_0s.png)
laravel接管Dingo-api和默认的错误处理方式
2023-11-21 23:29:37
![](https://img.aspxhome.com/file/2023/6/111006_0s.jpg)
全面解读Python Web开发框架Django
2022-06-24 19:41:45
PHP调用全国天气预报数据接口查询天气示例
2023-11-14 03:11:49
pandas删除部分数据后重新生成索引的实现
2023-11-18 04:44:13
![](https://img.aspxhome.com/file/2023/9/99079_0s.png)
python3下载抖音视频的完整代码
2023-12-24 01:10:42