【发布时间】:2016-04-17 15:25:53
【问题描述】:
假设我们有一个虚拟函数:
async def foo(arg):
result = await some_remote_call(arg)
return result.upper()
两者有什么区别:
import asyncio
coros = []
for i in range(5):
coros.append(foo(i))
loop = asyncio.get_event_loop()
loop.run_until_complete(asyncio.wait(coros))
还有:
import asyncio
futures = []
for i in range(5):
futures.append(asyncio.ensure_future(foo(i)))
loop = asyncio.get_event_loop()
loop.run_until_complete(asyncio.wait(futures))
注意:示例返回结果,但这不是问题的重点。当返回值很重要时,使用gather() 而不是wait()。
无论返回值如何,我都在寻找ensure_future() 的清晰度。 wait(coros) 和 wait(futures) 都运行协程,那么协程何时以及为什么应该包含在 ensure_future 中?
基本上,使用 Python 3.5 的 async 运行一堆非阻塞操作的正确方法 (tm) 是什么?
为了获得额外的信用,如果我想批量调用怎么办?例如,我需要调用some_remote_call(...) 1000 次,但我不想用 1000 个同时连接来破坏 Web 服务器/数据库/等。这对于线程或进程池是可行的,但是有没有办法使用asyncio 来做到这一点?
2020 年更新(Python 3.7+):不要使用这些 sn-ps。而是使用:
import asyncio
async def do_something_async():
tasks = []
for i in range(5):
tasks.append(asyncio.create_task(foo(i)))
await asyncio.gather(*tasks)
def do_something():
asyncio.run(do_something_async)
还可以考虑使用 Trio,这是 asyncio 的强大的第 3 方替代方案。
【问题讨论】:
标签: python python-3.x python-asyncio