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

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

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

MaxOmnis   2021-03-21 我要评论
想了解python协程用法实例分析的相关内容吗,MaxOmnis在本文为您仔细讲解python协程用法的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点: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程序设计有所帮助。

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们