外部库函数的异步并行性未标记为异步

asyncio parallelism of external library function not marked async

我需要使用 asyncio 并行发送请求。这些请求是通过我无法更改的库中的函数 server_time 发送的。它是一个函数,不是一个协程,所以不能等待它,这是这里的挑战。

我一直在尝试使用下面的代码,但它显然不起作用,同样的原因你可以并行化 await asyncio.sleep(1) 但不能并行化 time.sleep(1).

如何使用 asyncio 来并行化它?即,我如何使用 asyncio 并行化 time.sleep(1) 之类的东西?

from pybit import HTTP
import time
import asyncio

session = HTTP('https://api-testnet.bybit.com')

async def latency():
    time_1 = time.perf_counter()
    session.server_time()
    return time.perf_counter() - time_1

async def avg_latency(n_requests):
    total_time = 0
    tasks = []
    for _ in range(n_requests):
        tasks.append(asyncio.create_task(latency()))
    for task in tasks:
        total_time += await task
    return total_time / n_requests

# First one to establish the connection. The latency improves after.
session.server_time()

latency = asyncio.run(avg_latency(10))
print(f'{1000 * latency:.2f} ms')

您可以使用 run_in_executor 到 运行 同步,I/O-bound session.server_time 函数并行:

import asyncio
from pybit import HTTP
import time

session = HTTP('https://api-testnet.bybit.com')

async def latency():
   time_1 = time.perf_counter()
   loop = asyncio.get_running_loop()
   await loop.run_in_executor(None, session.server_time)
   return time.perf_counter() - time_1

async def avg_latency(n_requests):
   return sum(await asyncio.gather(*[latency() for _ in range(n_requests)]))/n_requests

print(asyncior.run(avg_latency(10)))