《火与遗忘》蟒蛇异步/等待 [英] "Fire and forget" python async/await
问题描述
有时需要进行一些非关键的异步操作,但我不想等待它完成.在 Tornado 的协程实现中,您可以通过简单地省略 yield
关键字来触发并忘记"异步函数.
Sometimes there is some non-critical asynchronous operation that needs to happen but I don't want to wait for it to complete. In Tornado's coroutine implementation you can "fire & forget" an asynchronous function by simply ommitting the yield
key-word.
我一直在试图弄清楚如何使用 Python 3.5 中发布的新 async
/await
语法触发并忘记".例如,一个简化的代码片段:
I've been trying to figure out how to "fire & forget" with the new async
/await
syntax released in Python 3.5. E.g., a simplified code snippet:
async def async_foo():
print("Do some stuff asynchronously here...")
def bar():
async_foo() # fire and forget "async_foo()"
bar()
结果是 bar()
永远不会执行,而是我们收到运行时警告:
What happens though is that bar()
never executes and instead we get a runtime warning:
RuntimeWarning: coroutine 'async_foo' was never awaited
async_foo() # fire and forget "async_foo()"
推荐答案
更新:
如果您使用 Python,则在任何地方将 asyncio.ensure_future
替换为 asyncio.create_task
>= 3.7 这是一种更新、更好的方法 生成任务.
Replace asyncio.ensure_future
with asyncio.create_task
everywhere if you're using Python >= 3.7 It's a newer, nicer way to spawn tasks.
根据asyncio.Task<的python文档/code>
可以启动一些协程来在后台"执行.由 asyncio.ensure_future
<创建的任务/a> 不会阻塞执行(因此函数会立即返回!).这看起来像是一种即发即忘"的方式.按照您的要求.
According to python docs for asyncio.Task
it is possible to start some coroutine to execute "in the background". The task created by asyncio.ensure_future
won't block the execution (therefore the function will return immediately!). This looks like a way to "fire and forget" as you requested.
import asyncio
async def async_foo():
print("async_foo started")
await asyncio.sleep(1)
print("async_foo done")
async def main():
asyncio.ensure_future(async_foo()) # fire and forget async_foo()
# btw, you can also create tasks inside non-async funcs
print('Do some actions 1')
await asyncio.sleep(1)
print('Do some actions 2')
await asyncio.sleep(1)
print('Do some actions 3')
if __name__ == '__main__':
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
输出:
Do some actions 1
async_foo started
Do some actions 2
async_foo done
Do some actions 3
如果在事件循环完成后执行任务怎么办?
请注意,asyncio 期望任务在事件循环完成时完成.因此,如果您将 main()
更改为:
async def main():
asyncio.ensure_future(async_foo()) # fire and forget
print('Do some actions 1')
await asyncio.sleep(0.1)
print('Do some actions 2')
程序完成后您会收到此警告:
You'll get this warning after the program finished:
Task was destroyed but it is pending!
task: <Task pending coro=<async_foo() running at [...]
为了防止这种情况,您可以在事件循环完成后等待所有挂起的任务:
To prevent that you can just await all pending tasks after the event loop has completed:
async def main():
asyncio.ensure_future(async_foo()) # fire and forget
print('Do some actions 1')
await asyncio.sleep(0.1)
print('Do some actions 2')
if __name__ == '__main__':
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
# Let's also finish all running tasks:
pending = asyncio.Task.all_tasks()
loop.run_until_complete(asyncio.gather(*pending))
杀死任务而不是等待它们
有时您不想等待任务完成(例如,某些任务可能被创建为永远运行).在这种情况下,您可以直接 cancel()
而不是等待它们:
import asyncio
from contextlib import suppress
async def echo_forever():
while True:
print("echo")
await asyncio.sleep(1)
async def main():
asyncio.ensure_future(echo_forever()) # fire and forget
print('Do some actions 1')
await asyncio.sleep(1)
print('Do some actions 2')
await asyncio.sleep(1)
print('Do some actions 3')
if __name__ == '__main__':
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
# Let's also cancel all running tasks:
pending = asyncio.Task.all_tasks()
for task in pending:
task.cancel()
# Now we should await task to execute it's cancellation.
# Cancelled task raises asyncio.CancelledError that we can suppress:
with suppress(asyncio.CancelledError):
loop.run_until_complete(task)
输出:
Do some actions 1
echo
Do some actions 2
echo
Do some actions 3
echo
这篇关于《火与遗忘》蟒蛇异步/等待的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!