利用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. css flex 使内容 水平居中 的方法...

    刚开始以为是  justify-content : center 设置为 居中... 的确,,当 元素满了时 的确能 居中.但是 当只有一个元素时,这一个元素也会居中... 想了半天没找到方法..突然 ...

  2. [HNOI/AHOI2018]排列

    [Luogu4437] 如果\(a[i]=j\)则序列\(p[]\)中\(j\)必须排在\(i\)前面,如果\(j\)不在范围内则不管,求一个式子\(\sum_{i=1}^n iw_{p[i]}\)的 ...

  3. ZOJ Monthly, January 2019 I Little Sub and Isomorphism Sequences(set 妙用) ZOJ4089

    写这篇博客来证明自己的愚蠢 ...Orz  飞机 题意:给定你个数组,以及一些单点修改,以及询问,每次询问需要求得,最长的字串长度,它在其他位置存在同构 题解:经过一些奇思妙想后 ,你可以发现问题是传 ...

  4. AtCoder Beginner Contest 113 B

    B - Palace Time limit : 2sec / Memory limit : 1024MB Score: 200 points Problem Statement A country d ...

  5. Codeforces - 915E 离散化区间覆盖

    我一直以来都错认为离散化就是换个映射,其实还需要在离散值两端加上相差为1的值才能真正离散 不然看一下test3就知道 不过这个离散姿势太暴力,以至于我1000ms时限跑出998ms(其实是太懒没有删重 ...

  6. pip安装时的异常,找不到lib2to3\\Grammar.txt

    [From] http://jahu.iteye.com/blog/2353325 异常 : [Errno 2] No such file or directory: 'd:\\python\\pyt ...

  7. postgresql 的一些操作

    (4)常用数据库命令(mysql为MySQL数据库操作命令,psql为postgresql数据库命令) 手动重启数据库命令:pg_ctl -D /usr/local/var/postgres -l / ...

  8. PIE SDK地图平移校正

    地图平移校正,当加载两幅空间参考一样的影像,其中一幅影像有点偏移,这时就以另一幅影像为基准将其进行平移校正,然后保存,再次加载就不会出现偏移了. 下面来介绍下实现的主要代码: 首先通过选中目录树中的要 ...

  9. C++指针传递和引用传递的区别 (转载整理)

    区别1:指针传递和引用传递是以不同的方式实现相同的效果,但是指针传递的本质还是值传递,只是传递的值是地址. 就拿 交换两个数的函数来举例: // 指针传递 void swap(int * val1, ...

  10. oracle 查看一个表中的记录是否被锁住

    SELECT a.object_id, a.session_id, b.object_nameFROM v$locked_object a, dba_objects bWHERE a.object_i ...