Last active
April 2, 2023 07:02
-
-
Save mikeckennedy/c76739766ce072f980aa4df1a6dc9516 to your computer and use it in GitHub Desktop.
Convert an async method to a synchronous one.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import asyncio | |
import functools | |
from typing import Any, Coroutine | |
loop = asyncio.new_event_loop() | |
def run(async_coroutine: Coroutine[Any, Any, Any]): | |
""" | |
Convert an async method to a synchronous one. | |
Example: | |
async def some_async_method(x, y): ... | |
result = syncify.run( some_async_method(1, 2) ) | |
Args: | |
async_coroutine (): | |
Returns: The value returned by `async_coroutine` | |
""" | |
return loop.run_until_complete(async_coroutine) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You're welcome. You might look into what they are suggesting in this Stackoverflow answer:
https://stackoverflow.com/questions/39400885/await-for-any-future-asyncio/39407084#39407084