python 协程 php,python协程用法实例分析

本文实例讲述了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程序设计有所帮助。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值