在python里使用await关键字来等另外一个协程的实例

 更新时间:2020年05月04日 17:59:08   作者:caimouse  
这篇文章主要介绍了在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

以上这篇在python里使用await关键字来等另外一个协程的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • python协程之动态添加任务的方法

    python协程之动态添加任务的方法

    今天小编就为大家分享一篇python协程之动态添加任务的方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2019-02-02
  • Python数学建模StatsModels统计回归之线性回归示例详解

    Python数学建模StatsModels统计回归之线性回归示例详解

    这篇文章主要为大家介绍了Python数学建模中StatsModels统计回归之线性回归的示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助
    2021-10-10
  • Python入门之面向对象和类

    Python入门之面向对象和类

    这篇文章主要为大家介绍了Python面向对象和类,具有一定的参考价值,感兴趣的小伙伴们可以参考一下,希望能够给你带来帮助
    2022-01-01
  • python 解决微分方程的操作(数值解法)

    python 解决微分方程的操作(数值解法)

    这篇文章主要介绍了python 解决微分方程的操作(数值解法),具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-05-05
  • python 保存float类型的小数的位数方法

    python 保存float类型的小数的位数方法

    今天小编就为大家分享一篇python 保存float类型的小数的位数方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-10-10
  • python3+PyQt5实现拖放功能

    python3+PyQt5实现拖放功能

    这篇文章主要为大家详细介绍了python3+PyQt5实现拖放功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-04-04
  • 教你在Excel中调用Python脚本实现数据自动化处理的方法

    教你在Excel中调用Python脚本实现数据自动化处理的方法

    Excel是全世界最流行的编程语言,Excel已经可以实现编程语言的算法,因此它是具备图灵完备性的,和JavaScript、Java、Python一样,今天通过本文给大家介绍下Python数据自动化处理的相关知识,感兴趣的朋友一起看看吧
    2022-02-02
  • PyTorch搭建LSTM实现多变量时序负荷预测

    PyTorch搭建LSTM实现多变量时序负荷预测

    这篇文章主要为大家介绍了PyTorch搭建LSTM实现多变量时间序列预测及负荷预测,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-05-05
  • Python超有趣实例通过冒泡排序来实现LOL厄斐琉斯控枪

    Python超有趣实例通过冒泡排序来实现LOL厄斐琉斯控枪

    冒泡排序是一种简单的排序算法,它也是一种稳定排序算法。其实现原理是重复扫描待排序序列,并比较每一对相邻的元素,当该对元素顺序不正确时进行交换。一直重复这个过程,直到没有任何两个相邻元素可以交换,就表明完成了排序
    2022-05-05
  • 介绍Python的Django框架中的静态资源管理器django-pipeline

    介绍Python的Django框架中的静态资源管理器django-pipeline

    这篇文章主要介绍了介绍Python的Django框架中的静态资源管理器django-pipeline,django-pipeline是一个开源项目,被用来处理css等静态文件,需要的朋友可以参考下
    2015-04-04

最新评论