Last active
January 12, 2021 05:59
-
-
Save CaledoniaProject/1748056ca68b33e9e4326543d48af863 to your computer and use it in GitHub Desktop.
Run async methods from non-async methods
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
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