Last active
June 27, 2024 08:25
-
-
Save PaleNeutron/3c85fb5f2bb7d2f9b76e1ebef2afe82e to your computer and use it in GitHub Desktop.
Run python async function in sync function
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 | |
from asgiref.sync import async_to_sync | |
async def async_func(value): | |
asyncio.sleep(0.1) | |
return value | |
def nested_sync_func(): | |
ret = async_to_sync(async_func)() | |
return ret | |
async def main(): | |
"""main is async and started as normal with asyncio.run""" | |
print("BEGIN main") | |
ret = nested_sync_func() | |
print(ret) | |
if __name__ == "__main__": | |
asyncio.run(main()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment