'asyncio parallelism of external library function not marked async

I need to send requests in parallel using asyncio. The requests are sent via the function server_time from a library that I can't change. It's a function, not a coroutine, so can't await it, which is the challenge here.

I've been trying with the code below but it doesn't work obviously, same reason you can parallelize await asyncio.sleep(1) but not time.sleep(1).

How do I use asyncio to parallelize this? I.e., how can I parallelize something like time.sleep(1) using asyncio?

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


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source