如何在 Python 3.5 中使用 async/await?
How to use async/await in Python 3.5?
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import time
async def foo():
await time.sleep(1)
foo()
我无法将这个简单的例子写成 运行:
RuntimeWarning: coroutine 'foo' was never awaited foo()
运行 协程需要一个 事件循环 。使用 asyncio()
library 创建一个:
import asyncio
# Python 3.7+
asyncio.run(foo())
或
# Python 3.6 and older
loop = asyncio.get_event_loop()
loop.run_until_complete(foo())
另见Tasks and Coroutines chapter of the asyncio
documentation。如果您已经有一个循环 运行ning,您希望通过创建一个任务(Python 3.7+ 中的 asyncio.create_task(...)
,asyncio.ensure_future(...)
同时 运行 其他协程在旧版本中)。
但是请注意,time.sleep()
不是 可等待对象。它 returns None
所以你在 1 秒后得到一个异常:
>>> asyncio.run(foo())
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/.../lib/python3.7/asyncio/runners.py", line 43, in run
return loop.run_until_complete(main)
File "/.../lib/python3.7/asyncio/base_events.py", line 573, in run_until_complete
return future.result()
File "<stdin>", line 2, in foo
TypeError: object NoneType can't be used in 'await' expression
在这种情况下,您应该改用 asyncio.sleep()
coroutine:
async def foo():
await asyncio.sleep(1)
配合循环使其他任务运行。要阻止来自没有 asyncio 等效项的第三方库的代码,您可以 运行 asyncio 开发指南中 executor pool. See Running Blocking Code 中的代码。
如果您已经有一个循环 运行(与其他一些任务),您可以添加新任务:
asyncio.ensure_future(foo())
否则你可能会得到
The event loop is already running
错误。
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import time
async def foo():
await time.sleep(1)
foo()
我无法将这个简单的例子写成 运行:
RuntimeWarning: coroutine 'foo' was never awaited foo()
运行 协程需要一个 事件循环 。使用 asyncio()
library 创建一个:
import asyncio
# Python 3.7+
asyncio.run(foo())
或
# Python 3.6 and older
loop = asyncio.get_event_loop()
loop.run_until_complete(foo())
另见Tasks and Coroutines chapter of the asyncio
documentation。如果您已经有一个循环 运行ning,您希望通过创建一个任务(Python 3.7+ 中的 asyncio.create_task(...)
,asyncio.ensure_future(...)
同时 运行 其他协程在旧版本中)。
但是请注意,time.sleep()
不是 可等待对象。它 returns None
所以你在 1 秒后得到一个异常:
>>> asyncio.run(foo())
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/.../lib/python3.7/asyncio/runners.py", line 43, in run
return loop.run_until_complete(main)
File "/.../lib/python3.7/asyncio/base_events.py", line 573, in run_until_complete
return future.result()
File "<stdin>", line 2, in foo
TypeError: object NoneType can't be used in 'await' expression
在这种情况下,您应该改用 asyncio.sleep()
coroutine:
async def foo():
await asyncio.sleep(1)
配合循环使其他任务运行。要阻止来自没有 asyncio 等效项的第三方库的代码,您可以 运行 asyncio 开发指南中 executor pool. See Running Blocking Code 中的代码。
如果您已经有一个循环 运行(与其他一些任务),您可以添加新任务:
asyncio.ensure_future(foo())
否则你可能会得到
The event loop is already running
错误。