version :python3.7
asyncio 是python 3 里面炙手可热的库,配合async/await 两个命令字,给python工程化代码带来更强大的生命力。本文主要是通过学习asyncio标准库的使用,更熟练python的异步多线程编程。
核心步骤
- 创建asyncio 的event loop
- 调用 async/await 函数
- 创建task在loop上运行
- 等待多个tasks运行结束
- 关闭loop
第一个asyncio程序
import time
async def main():
print(f"{time.ctime()},hello")
await asyncio.sleep(1.0)
print(f"{time.ctime()},goodbye")
asyncio.run(main())
asyncio.run()
包括了以上5个核心的步骤,为了方便我们学习asyncio
这个库,可以把它拆分开。在实际写代码的过程中,可能会遇到更复杂的处理,所以拆分后的步骤,可以自己根据实际情况灵活处理。示例中既有异步的main也有同步的blocking。
import asyncio
import time
async def main():
print(f"{time.ctime()},hello")
await asyncio.sleep(10.0)
print(f"{time.ctime()},goodbye")
def blocking():
time.sleep(5.0)
print(f"{time.ctime()} From thread")
# 创建loop
loop = asyncio.get_event_loop()
# 创建task
task = loop.create_task(main())
# 开启新的thread运行blocking任务
# 注意:这里阻塞式任务的sleep时间比异步的sleep时间短,可以正常运营,否则异步的loop会提前退出,导致loop报错。
loop.run_in_executor(None, blocking)
# run_until_complete 才开始执行上面创建的异步task
loop.run_until_complete(task)
pending = asyncio.all_tasks(loop=loop)
for task in pending:
task.cancel()
# 等待所有的task执行结束
group = asyncio.gather(*pending, return_exceptions=True)
loop.run_until_complete(group)
# 关闭loop
loop.close()
运行结果
Fri Mar 13 22:47:39 2020,hello
Fri Mar 13 22:47:49 2020 From thread
Fri Mar 13 22:47:59 2020,goodbye
运行的时候查看thread状态,可以看到确实有2个thread。LWP(Light Weight Process)
看到线程的id,NLWP(number of Light Weight Process)
线程的数量为2。
ps -efL|grep run_in_asyncio.py
UID PID PPID LWP C NLWP STIME TTY TIME CMD
mqq 136552 106436 136552 1 2 22:47 pts/10 00:00:00 python run_in_asyncio.py
mqq 136552 106436 136572 0 2 22:47 pts/10 00:00:00 python run_in_asyncio.py
网友评论