All my code is written without asyncio in mind; however, I use one function that is async (written by another developer; for my purposes it's a black box). Let's call this func_1. I need to call this function from within another function, call it func_2 (which itself may be called in an arbitrarily long chain of functions func_3, func_4 etc...).
Since func_1 is async, I need to await it, but since I call it in func_2, I need to make func_2 async as well (I can't await within a non-async function). And this goes on an on; I need to turn the entire chain of functions func_2, func_3, func_4 into async functions.
Is there a way to avoid this? I just want to call func_1, wait for it to finish, and use the results in the rest of my normal python code. Can I create a wrapper around func_1 to allow this?
What I want is essentially the following, which doesn't work:
# This is the function defined by someone else
async def func_1(*args):
return something(*args)
# This is my wrapper
def func_1_wrapper(*args):
return await func_1(*args)
# So that I can call it like normal within the rest of my code
def func_2(*args):
# do something
a = func_1_wrapper(*args)
# do something else