asyncio标准库1 Hello World
利用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的更多相关文章
- python协程(yield、asyncio标准库、gevent第三方)、异步的实现
引言 同步:不同程序单元为了完成某个任务,在执行过程中需靠某种通信方式以协调一致,称这些程序单元是同步执行的. 例如购物系统中更新商品库存,需要用"行锁"作为通信信号,让不同的更新 ...
- asyncio标准库7 Producer/consumer
使用asyncio.Queue import asyncio import random async def produce(queue, n): for x in range(1, n + 1): ...
- asyncio标准库6 Threads & Subprocess
Threads import asyncio def compute_pi(digits): # implementation return 3.14 async def main(loop): di ...
- asyncio标准库5 TCP echo client and server
server import asyncio async def handle_echo(reader, writer): data = await reader.read(100) message = ...
- asyncio标准库4 asyncio performance
性能包括2部分 每秒并发请求数(Number of concurrent requests per second) 每秒请求负载(Request latency in seconds: min/ave ...
- asyncio标准库3 HTTP client example
import aiohttp import asyncio import async_timeout async def fetch(session, url): async with async_t ...
- asyncio标准库2 Hello Clock
如何调度协程,并发运行 asyncio.gather方法可以聚合协程or future def gather(*coros_or_futures, loop=None, return_exceptio ...
- 转--Python标准库之一句话概括
作者原文链接 想掌握Python标准库,读它的官方文档很重要.本文并非此文档的复制版,而是对每一个库的一句话概括以及它的主要函数,由此用什么库心里就会有数了. 文本处理 string: 提供了字符集: ...
- Python 标准库一览(Python进阶学习)
转自:http://blog.csdn.net/jurbo/article/details/52334345 写这个的起因是,还是因为在做Python challenge的时候,有的时候想解决问题,连 ...
随机推荐
- 大佬的编码建议,让你的代码更pythonic
大佬的编码建议,让你的代码更pythonic Raymond Hettinger是 Python 核心开发者,本文提到的许多特性都是他开发的. 若无例外,本文代码中出现的 colors names d ...
- [一首诗] Life and Death 生与死
Life and Death 生与死 I strove with none, 我和谁都不争, for none was worth my strife, 和谁争我都不屑: Nature I loved ...
- Celery 大量任务 分发
Celery是由Python开发的一个简单.灵活.可靠的处理大量任务的分发系统,它不仅支持实时处理也支持任务调度. user:用户程序,用于告知celery去执行一个任务. broker: 存放任务( ...
- SPOJ - DISUBSTR 求串中子串的个数
\(height\)简单应用 #include<iostream> #include<cstdio> #include<cstring> #include<c ...
- 页面加载时的div动画
用@keyframes(动画),实现页面加载时的div动画(不要用js控制,因为当页面加载的时候,js还不一定可以使用) 可以在https://daneden.github.io/animate.cs ...
- python 生成嵌套字典
import collections import json tree=lambda:collections.defaultdict(tree) some_dict=tree() some_dict[ ...
- Apache Beam的基本概念
不多说,直接上干货! Apache Beam的基本概念 在使用Apache Beam构建数据处理程序,首先需要使用Beam SDK中的类创建一个Driver程序,在Driver程序中创建一个满足我们数 ...
- VMware虚拟网卡设置问题
具体操作过程如下: (1)为虚拟机添加虚拟网卡 (2)添加后会自动分配子网ip,不用修改.点击应用,确定. (3)添加完成后本机的网络上会多出一个网络适配器,根据虚拟机器中的ip设置此ip地址, 这里 ...
- DotNetCore跨平台~xUnit和测试报告
在进入dotnet core时代之后,测试驱动开发TDD的主要工具不再是微软的nunit,取而代之的是更通用的xunit,微软把它集成到了dotnetcore的项目里,在安装完成vs2017之后,你可 ...
- unity3d发布到安卓平台
1.首先你得装上JDK并且配置好环境(就像学java配置环境一样) 百度jdk把下载安装成功 找到安装jdk目录的bin目录,复制路径,例如 C:\Program Files (x86)\Java\j ...