利用asyncio的event loop,编写和调度协程

coroutine [,kəuru:'ti:n] n. 协程

Simple coroutine(调用1个协程)

import asyncio

async def say(what, when):
await asyncio.sleep(when)
print(what) loop = asyncio.get_event_loop()
loop.run_until_complete(say('hello world', 1)) # 使用run_until_complete()方法,在协程完成后中断event loop。
loop.close()

Creating tasks(调用多个协程)

import asyncio

async def say(what, when):
await asyncio.sleep(when)
print(what) loop = asyncio.get_event_loop() loop.create_task(say('first hello', 2))
loop.create_task(say('second hello', 1)) loop.run_forever() # 使用run_forever()方法,协程会一直运行,不会中断event loop
loop.close()

Stopping the loop

import asyncio

async def say(what, when):
await asyncio.sleep(when)
print(what) async def stop_after(loop, when):
await asyncio.sleep(when)
loop.stop() # 中断event loop loop = asyncio.get_event_loop() loop.create_task(say('first hello', 2))
loop.create_task(say('second hello', 1))
loop.create_task(say('third hello', 4))
loop.create_task(stop_after(loop, 3)) loop.run_forever()
loop.close()
# out:
second hello
first hello
Task was destroyed but it is pending!
task: <Task pending coro=<say() done, defined at e03.py:5> wait_for=<Future pending cb=[<TaskWakeupMethWrapper object at 0x7fed59595a68>()]>> # 在执行2个任务后,中断event loop,'third hello‘任务由于延迟时间4秒,未能执行。

asyncio标准库1 Hello World的更多相关文章

  1. python协程(yield、asyncio标准库、gevent第三方)、异步的实现

    引言 同步:不同程序单元为了完成某个任务,在执行过程中需靠某种通信方式以协调一致,称这些程序单元是同步执行的. 例如购物系统中更新商品库存,需要用"行锁"作为通信信号,让不同的更新 ...

  2. asyncio标准库7 Producer/consumer

    使用asyncio.Queue import asyncio import random async def produce(queue, n): for x in range(1, n + 1): ...

  3. asyncio标准库6 Threads & Subprocess

    Threads import asyncio def compute_pi(digits): # implementation return 3.14 async def main(loop): di ...

  4. asyncio标准库5 TCP echo client and server

    server import asyncio async def handle_echo(reader, writer): data = await reader.read(100) message = ...

  5. asyncio标准库4 asyncio performance

    性能包括2部分 每秒并发请求数(Number of concurrent requests per second) 每秒请求负载(Request latency in seconds: min/ave ...

  6. asyncio标准库3 HTTP client example

    import aiohttp import asyncio import async_timeout async def fetch(session, url): async with async_t ...

  7. asyncio标准库2 Hello Clock

    如何调度协程,并发运行 asyncio.gather方法可以聚合协程or future def gather(*coros_or_futures, loop=None, return_exceptio ...

  8. 转--Python标准库之一句话概括

    作者原文链接 想掌握Python标准库,读它的官方文档很重要.本文并非此文档的复制版,而是对每一个库的一句话概括以及它的主要函数,由此用什么库心里就会有数了. 文本处理 string: 提供了字符集: ...

  9. Python 标准库一览(Python进阶学习)

    转自:http://blog.csdn.net/jurbo/article/details/52334345 写这个的起因是,还是因为在做Python challenge的时候,有的时候想解决问题,连 ...

随机推荐

  1. C++_标准模板库STL概念介绍3-函数对象

    函数对象也叫做函数符(functor). 函数符是可以以函数方式和( )结合使用的任意对象. 包括函数名,指向函数的指针,重载了()运算符的类对象. 可以这样定义一个类: class Linear { ...

  2. paraview添加vector

    https://youtu.be/cygVdhn-kG0 (须自行FQ)

  3. 使用Git向GitHub上上传代码

    参考:http://www.cnblogs.com/yxhblogs/p/6130995.html 如果遇到[git无法pull仓库refusing to merge unrelated histor ...

  4. Permutation(构造+思维)

    A permutation p is an ordered group of numbers p1,   p2,   ...,   pn, consisting of ndistinct positi ...

  5. wx.getLocation和show-location定位点不符

    发现开发者工具未发现此类问题,到了真机上预览,观察到wx.getLocation的经纬度和show-location定位点的位置不符合.该怎么解决? 开发者工具上: 真机上: 解决方法: getLoc ...

  6. Java 不可变对象

    不可变对象(immutable objects):一旦对象被创建,它们的状态就不能被改变(包括基本数据类型的值不能改变,引用类型的变量不能指向其他的对象,引用类型指向的对象的状态也不能改变),每次对他 ...

  7. html中一个div怎么引入另一个页面

    转载:https://zhidao.baidu.com/question/588973997598951645.html

  8. 第四次 Scrum Meeting

    第四次 Scrum Meeting 写在前面 会议时间 会议时长 会议地点 2019/4/8 22:00 30min 大运村1号楼3F 附Github仓库:WEDO 例会照片 工作情况总结(4.8) ...

  9. 行业UI设计师总结UI设计8个趋势

    纵观整个设计的历史可以发现,设计的历史演变都无一例外都是从复杂的装饰性设计逐步的演化为更加注重功能性的简洁化设计.并且设计师们还在不停的试图通过各种设计语言的创新追求极至设计的可能性.设计潮流变更的核 ...

  10. rsync 问题总结

    Rsync服务常见问题汇总讲解:==================================1. rsync服务端开启的iptables防火墙  [客户端的错误]   No route to ...