繁体   English   中英

在 python asyncio 中并发运行函数

[英]Running functions concurrently in python asyncio

我正在使用 python 3.6 并尝试使用 asyncio 并发运行任务。 我认为 asyncio.gather 并确保未来将成为使用的工具,但它似乎并没有像我想象的那样工作。 有人可以给我指点吗?

这是我的代码:

import time
import asyncio

async def f1():
  print('Running func 1')
  time.sleep(4)
  print('Returning from func 1')
  return 1

async def f2():
  print('Running func 2')
  time.sleep(6)
  print('Returning from func 2')
  return 2

async def f3():
  print('Running func 3')
  time.sleep(1)
  print('Returning from func 3')
  return 3

async def foo():
  calls = [
    asyncio.ensure_future(f())
    for f in [f1, f2, f3]
  ]

  res = await asyncio.gather(*calls)

  print(res)

loop = asyncio.get_event_loop()
start = time.time()
loop.run_until_complete(foo())
end = time.time()
print(f'Took {end - start} seconds')
print('done')

我希望这 3 个功能彼此独立运行,但每个功能似乎都被阻止在另一个之后。 这是我得到的输出

Running func 1
Returning from func 1
Running func 2
Returning from func 2
Running func 3
Returning from func 3
[1, 2, 3]
Took 11.009816884994507 seconds
done

我原以为它需要 6 秒,瓶颈是 f2。

首先,欢迎来到 StackOverflow。

当您在事件循环中运行代码时,如果您不想阻塞整个过程,则此代码必须使用异步库或在执行程序中运行。

通过这种方式,事件循环可以发送要在工作线程中或事件循环本身中执行的任务背景,以防您使用异步库。 同时,事件循环可以处理新函数或代码的一部分并重复相同的过程。

一旦任何后台任务完成,事件循环就会捕获它们并返回其值。

在您的情况下,如果您使用异步睡眠库,您应该获得预期的结果。 例如:

  async def f1():
      print('Running func 1')
      await asyncio.sleep(4)
      print('Returning from func 1')
      return 1

我没有阅读本教程,但我希望找到您的解决方案应该很有趣。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM