有时,有一些非关键的异步操作需要发生,但我不想等待它完成。在Tornado的Coroutine实施中,您可以通过简单地使用该功能来"开火&忘记"异步功能yield
关键词。
我一直在努力弄清楚如何使用新的async
/await
语法在Python 3.5中发布。例如,简化的代码段:
async def async_foo():
print("Do some stuff asynchronously here...")
def bar():
async_foo() # fire and forget "async_foo()"
bar()
虽然发生了什么bar()
永远不要执行,而是我们会收到运行时警告:
RuntimeWarning: coroutine 'async_foo' was never awaited
async_foo() # fire and forget "async_foo()"
答案
Upd:
代替asyncio.ensure_future
和asyncio.create_task
如果您使用python> = 3.7,到处都是一种更新的,更好的方法产生任务。
Asyncio.Task"开火和忘记"
根据Python文档asyncio.Task
有可能启动一些Coroutineexecute “in the background” 。由asyncio.ensure_future
不会阻止执行(因此该函数将立即返回!)。这看起来像是您要求的一种"开火和忘记"的方法。
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')
程序完成后,您将获得此警告:
Task was destroyed but it is pending!
task: <Task pending coro=<async_foo() running at [...]
为了防止你可以等待所有待处理的任务事件循环完成后:
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