在python里使用await关键字来等另外一个协程的实例
作者:caimouse 发布时间:2021-03-01 19:10:16
标签:python,await,关键字,协程
一个协程里可以启动另外一个协程,并等待它完成返回结果,采用await关键字,
例子如下:
import asyncio
async def outer():
print('in outer')
print('waiting for result1')
result1 = await phase1()
print('waiting for result2')
result2 = await phase2(result1)
return (result1, result2)
async def phase1():
print('in phase1')
return 'result1'
async def phase2(arg):
print('in phase2')
return 'result2 derived from {}'.format(arg)
event_loop = asyncio.get_event_loop()
try:
return_value = event_loop.run_until_complete(outer())
print('return value: {!r}'.format(return_value))
finally:
event_loop.close()
输出结果如下:
in outer
waiting for result1
in phase1
waiting for result2
in phase2
return value: ('result1', 'result2 derived from result1')
await关键字添加了一个新的协程到循环里,而不需要明确地添加协程到这个事件循环里。
补充知识:python里使用Condition对象来唤醒指定数量的协程
在asyncio库里,定义Condition对象,它的行为与事件Event有点像,区别是事件是通知所有对象,Condition对象可以指定一定数量的协程被通知,它是通过函数notify()来实现的,如果参数里放2,就是通知两个协程,例子如下:
import asyncio
async def consumer(condition, n):
with await condition:
print('consumer {} is waiting'.format(n))
await condition.wait()
print('consumer {} triggered'.format(n))
print('ending consumer {}'.format(n))
async def manipulate_condition(condition):
print('starting manipulate_condition')
# pause to let consumers start
await asyncio.sleep(0.1)
for i in range(1, 3):
with await condition:
print('notifying {} consumers'.format(i))
condition.notify(n=i)
await asyncio.sleep(0.1)
with await condition:
print('notifying remaining consumers')
condition.notify_all()
print('ending manipulate_condition')
async def main(loop):
# Create a condition
condition = asyncio.Condition()
# Set up tasks watching the condition
consumers = [
consumer(condition, i)
for i in range(5)
]
# Schedule a task to manipulate the condition variable
loop.create_task(manipulate_condition(condition))
# Wait for the consumers to be done
await asyncio.wait(consumers)
event_loop = asyncio.get_event_loop()
try:
result = event_loop.run_until_complete(main(event_loop))
finally:
event_loop.close()
结果输出如下:
starting manipulate_condition
consumer 2 is waiting
consumer 3 is waiting
consumer 4 is waiting
consumer 1 is waiting
consumer 0 is waiting
notifying 1 consumers
consumer 2 triggered
ending consumer 2
notifying 2 consumers
consumer 3 triggered
ending consumer 3
consumer 4 triggered
ending consumer 4
notifying remaining consumers
ending manipulate_condition
consumer 1 triggered
ending consumer 1
consumer 0 triggered
ending consumer 0
来源:https://blog.csdn.net/caimouse/article/details/77867169
0
投稿
猜你喜欢
- Mysql数据库备份和还原常用的命令是进行Mysql数据库备份和还原的关键,没有命令,什么都无从做起,更谈不上什么备份还原,只有给系统这个命
- gchart是基于google图表API的jquery组件。使用gchart可以方便地生成强大的各种图表和报表。基于google图表接口的g
- 一.链接打开方式1、新窗口打开优点:用户点链接的时候,当前浏览的内容不会被替换,不需要通过前进、后退去看自己看过的内容;缺点:访问一会,就会
- 本文实例讲述了Python删除windows垃圾文件的方法。分享给大家供大家参考。具体如下:#coding:utf-8import os#f
- asp在线备份sql server数据库: 1、备份sqlserver 代码如下:<% SQL="backup&n
- 一:目录操作 首先介绍的是一个从目录读取的函数,opendir(),readdir(),closedir(),使用的时候是先打开文件句柄,而
- 这些CSS Selector在平时写页面的时候用地不多,只在JavaScript库、Firefox插件、iPhone页面里有过接触。推荐大家
- CSS布局中可以用javascript判断浏览器版本看如下的javascript脚本: if (window.XMLHt
- microtime() 函数返回当前 Unix 时间戳的微秒数。用于检测程序执行时间的函数,也是PHP内置的时间函数之一,在PHP中可以用于
- 1、首先看SSM(Spring+SpringBoot+Mybatis)的依赖<project xmlns="http://m
- 本文实例讲述了Python创建xml文件的方法。分享给大家供大家参考,具体如下:这是一个使用ElementTree有关类库,生成xml文件的
- 今天在群(CSS森林:30247792)里讨论了border的样式写法,发现border的写法还真是灵活,做了一下总结,希望对大家有用:bo
- 如下所示:#统计某文件夹下的所有csv文件的行数(多线程)import threadingimport csvimport osclass
- 有一道题: 比较两个列表范围,如果包含的话,返回TRUE,否则FALSE。 详细题目如下:Create a function, this f
- 今早无聊。。。7点起来突然想写个刷访问量的。。那就动手吧仅供测试,不建议刷访问量哦~~很简单的思路,第一步提取代理ip,第二步模拟访问。提取
- 一般我们是利用Session对象来防止通过“刷新”增加计数器的访问量的,看看下面的做法;<%If IsEmpty(Sessi
- 最近使用Python 3.5写了一个GUI小程序,于是想将该写好的程序发布成一个exe文件,供自己单独使用。至于通过安装的方式使用该程序,我
- 前言都说抖音有毒,一刷就停不下来了。看来抖音这款产品紧紧抓住了人们内心深处的某些需求。当然今天不是来探讨抖音这款产品的啊。今天我们来学习如何
- 1. 数据处理中很恶心,出现 RuntimeWarning: divide by zero encountered in divide发现自
- 本文实例讲述了python对象及面向对象技术。分享给大家供大家参考,具体如下:1 先看一个例子. 本章将讲解这个例子程序:文件: filei