Skip to content

Instantly share code, notes, and snippets.

@PaleNeutron
Last active June 27, 2024 08:25
Show Gist options
  • Save PaleNeutron/3c85fb5f2bb7d2f9b76e1ebef2afe82e to your computer and use it in GitHub Desktop.
Save PaleNeutron/3c85fb5f2bb7d2f9b76e1ebef2afe82e to your computer and use it in GitHub Desktop.
Run python async function in sync function
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