So currently I have 4 api requests which are called in synchronous method using a third_party lib which is synchronous. What i want is to run them in parallel. So that my total time to call all 4 apis is reduced.
I am using fastapi as micro framework.
utilities.py
async def get_api_1_data():
data = some_third_party_lib()
return data
async def get_api_2_data():
data = some_third_party_lib()
return data
async def get_api_3_data():
data = some_third_party_lib()
return data
async def get_api_4_data():
data = some_third_party_lib()
return data
my main.py looks something like this
import asyncio
from fastapi import FastAPI
app = FastAPI()
@app.get("/")
async def fetch_new_exposure_api_data(node: str):
functions_to_run = [get_api_1_data(), get_api_2_data(), get_api_3_data(), get_api_4_data()]
r1, r2, r3, r4 = await asyncio.gather(*functions_to_run)
return [r1, r2, r3, r4]
So the issue is i can not put await in front of some_third_party_lib()
as it's not a async lib it's a sync lib.
So is there any way i can convert them to async functionality to run them in parallel.