Skip to content

Instantly share code, notes, and snippets.

@Saik0s
Created January 18, 2024 14:25
Show Gist options
  • Save Saik0s/263b7a4393238ba235df63b94c17e71d to your computer and use it in GitHub Desktop.
Save Saik0s/263b7a4393238ba235df63b94c17e71d to your computer and use it in GitHub Desktop.
async def run_async(func, *args, **kwargs) -> T:
"""
Runs a synchronous function in an asynchronous manner.
"""
async def wrapper() -> T:
try:
return await loop.run_in_executor(
None, functools.partial(func, *args, **kwargs)
)
except Exception as e:
# propagate the exception to the caller
raise e
loop = asyncio.get_event_loop()
return await wrapper()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment