Use the current loop instead of the main loop in case there is one available

#482
This commit is contained in:
Dan 2020-09-02 13:07:30 +02:00
parent 617f0c9b30
commit 44880f7efe

View File

@ -28,7 +28,7 @@ from pyrogram.methods.utilities import idle as idle_module
def async_to_sync(obj, name): def async_to_sync(obj, name):
function = getattr(obj, name) function = getattr(obj, name)
loop = asyncio.get_event_loop() main_loop = asyncio.get_event_loop()
async def consume_generator(coroutine): async def consume_generator(coroutine):
return [i async for i in 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): def async_to_sync_wrap(*args, **kwargs):
coroutine = function(*args, **kwargs) coroutine = function(*args, **kwargs)
try:
loop = asyncio.get_event_loop()
except RuntimeError:
loop = main_loop
if loop.is_running(): if loop.is_running():
if threading.current_thread() is threading.main_thread(): if threading.current_thread() is threading.main_thread():
return coroutine return coroutine