简体   繁体   English

在 python asyncio 中并发运行函数

[英]Running functions concurrently in python asyncio

I'm using python 3.6 and trying to use asyncio to run tasks concurrently.我正在使用 python 3.6 并尝试使用 asyncio 并发运行任务。 I thought asyncio.gather and ensure future would be the tools to use, but it does not seem to be working as I thought it would.我认为 asyncio.gather 并确保未来将成为使用的工具,但它似乎并没有像我想象的那样工作。 Could someone give me pointers?有人可以给我指点吗?

Here is my code:这是我的代码:

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')

I would expect the 3 functions to run independently of each other, but each one seems to be blocked behind the other.我希望这 3 个功能彼此独立运行,但每个功能似乎都被阻止在另一个之后。 This is the ouput I get这是我得到的输出

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

I would have expected it to take 6 seconds, with the bottleneck being f2.我原以为它需要 6 秒,瓶颈是 f2。

First off, welcome to StackOverflow.首先,欢迎来到 StackOverflow。

When you run code inside event loop, this code MUST use async libraries or be run in executor if you want not to block the entire process.当您在事件循环中运行代码时,如果您不想阻塞整个过程,则此代码必须使用异步库或在执行程序中运行。

In this way, event loop can send the task background to be executed in a worker or in the event loop itself in case you use async libraries.通过这种方式,事件循环可以发送要在工作线程中或事件循环本身中执行的任务背景,以防您使用异步库。 Meanwhile, event loop can attend new function o portion of code and repeat the same process.同时,事件循环可以处理新函数或代码的一部分并重复相同的过程。

Once any background task has finished, event loop catch them and return its value.一旦任何后台任务完成,事件循环就会捕获它们并返回其值。

In your case, if you use async library of sleep you should obtain expected results.在您的情况下,如果您使用异步睡眠库,您应该获得预期的结果。 For example:例如:

  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