From 44880f7efe31f157f88b32779e160682ea8b500c Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Wed, 2 Sep 2020 13:07:30 +0200 Subject: [PATCH] Use the current loop instead of the main loop in case there is one available #482 --- pyrogram/sync.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/pyrogram/sync.py b/pyrogram/sync.py index 80288e68..2bca9700 100644 --- a/pyrogram/sync.py +++ b/pyrogram/sync.py @@ -28,7 +28,7 @@ from pyrogram.methods.utilities import idle as idle_module def async_to_sync(obj, name): function = getattr(obj, name) - loop = asyncio.get_event_loop() + main_loop = asyncio.get_event_loop() async def consume_generator(coroutine): return [i async for i in coroutine] @@ -37,6 +37,11 @@ def async_to_sync(obj, name): def async_to_sync_wrap(*args, **kwargs): coroutine = function(*args, **kwargs) + try: + loop = asyncio.get_event_loop() + except RuntimeError: + loop = main_loop + if loop.is_running(): if threading.current_thread() is threading.main_thread(): return coroutine