Skip to content

Instantly share code, notes, and snippets.

@CaledoniaProject
Last active January 12, 2021 05:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CaledoniaProject/1748056ca68b33e9e4326543d48af863 to your computer and use it in GitHub Desktop.
Save CaledoniaProject/1748056ca68b33e9e4326543d48af863 to your computer and use it in GitHub Desktop.
Run async methods from non-async methods
class Test:
async def notify_async():
await asyncio.sleep(1)
def notify(self):
loop = None
try:
loop = asyncio.get_running_loop()
except RuntimeError:
loop = asyncio.new_event_loop()
asyncio.set_event_loop(asyncio.new_event_loop())
if loop.is_running():
loop.create_task(self.notify_async())
else:
loop.run_until_complete(self.notify_async())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment