Suppose there is a library that makes various database queries:
import time
def queryFoo():
time.sleep(4)
return "foo"
def queryBar():
time.sleep(4)
return "bar"
I want to execute those 2 queries concurrently without having to add async
to the method signature or adding a decorator. These functions should not depend on asyncio at all.
What is the best way to utilize those non-async functions within asyncio
?
I am looking for something of the form:
#I need an 'asyncWrapper'
results = asyncio.gather(asyncWrapper(queryFoo()), asyncWrapper(queryBar()))
Thank you in advance for your consideration and response.
run_in_executor()
, while I want to make sure the async function converted to sync such that I don't have nested async calls that have nested event loops. One of the solutions I can think of is to spawn new event loop for the inner async calls and block until its result got returned, and continue to next outer loops. Will that work? – Koniology