Stop storing asyncio loop in TelegramClient

The loop parameter was ignored because it shouldn't be used, but
the fact it still stored the current loop on creation messes up
with asyncio.run.
This commit is contained in:
Lonami Exo 2021-01-18 22:43:39 +01:00
parent 3ddb0a3903
commit de7cf03ba7
2 changed files with 8 additions and 9 deletions

View File

@ -242,7 +242,6 @@ class TelegramBaseClient(abc.ABC):
"Refer to telethon.rtfd.io for more information.") "Refer to telethon.rtfd.io for more information.")
self._use_ipv6 = use_ipv6 self._use_ipv6 = use_ipv6
self._loop = asyncio.get_event_loop()
if isinstance(base_logger, str): if isinstance(base_logger, str):
base_logger = logging.getLogger(base_logger) base_logger = logging.getLogger(base_logger)
@ -308,14 +307,14 @@ class TelegramBaseClient(abc.ABC):
# TODO A better fix is obviously avoiding the use of `sock_connect` # TODO A better fix is obviously avoiding the use of `sock_connect`
# #
# See https://github.com/LonamiWebs/Telethon/issues/1337 for details. # See https://github.com/LonamiWebs/Telethon/issues/1337 for details.
if not callable(getattr(self._loop, 'sock_connect', None)): if not callable(getattr(self.loop, 'sock_connect', None)):
raise TypeError( raise TypeError(
'Event loop of type {} lacks `sock_connect`, which is needed to use proxies.\n\n' 'Event loop of type {} lacks `sock_connect`, which is needed to use proxies.\n\n'
'Change the event loop in use to use proxies:\n' 'Change the event loop in use to use proxies:\n'
'# https://github.com/LonamiWebs/Telethon/issues/1337\n' '# https://github.com/LonamiWebs/Telethon/issues/1337\n'
'import asyncio\n' 'import asyncio\n'
'asyncio.set_event_loop(asyncio.SelectorEventLoop())'.format( 'asyncio.set_event_loop(asyncio.SelectorEventLoop())'.format(
self._loop.__class__.__name__ self.loop.__class__.__name__
) )
) )
@ -459,7 +458,7 @@ class TelegramBaseClient(abc.ABC):
# Join the task (wait for it to complete) # Join the task (wait for it to complete)
await task await task
""" """
return self._loop return asyncio.get_event_loop()
@property @property
def disconnected(self: 'TelegramClient') -> asyncio.Future: def disconnected(self: 'TelegramClient') -> asyncio.Future:
@ -532,7 +531,7 @@ class TelegramBaseClient(abc.ABC):
LAYER, self._init_request LAYER, self._init_request
)) ))
self._updates_handle = self._loop.create_task(self._update_loop()) self._updates_handle = self.loop.create_task(self._update_loop())
def is_connected(self: 'TelegramClient') -> bool: def is_connected(self: 'TelegramClient') -> bool:
""" """
@ -563,11 +562,11 @@ class TelegramBaseClient(abc.ABC):
# You don't need to use this if you used "with client" # You don't need to use this if you used "with client"
await client.disconnect() await client.disconnect()
""" """
if self._loop.is_running(): if self.loop.is_running():
return self._disconnect_coro() return self._disconnect_coro()
else: else:
try: try:
self._loop.run_until_complete(self._disconnect_coro()) self.loop.run_until_complete(self._disconnect_coro())
except RuntimeError: except RuntimeError:
# Python 3.5.x complains when called from # Python 3.5.x complains when called from
# `__aexit__` and there were pending updates with: # `__aexit__` and there were pending updates with:

View File

@ -309,14 +309,14 @@ class UpdateMethods:
channel_id = self._state_cache.get_channel_id(update) channel_id = self._state_cache.get_channel_id(update)
args = (update, others, channel_id, self._state_cache[channel_id]) args = (update, others, channel_id, self._state_cache[channel_id])
if self._dispatching_updates_queue is None: if self._dispatching_updates_queue is None:
task = self._loop.create_task(self._dispatch_update(*args)) task = self.loop.create_task(self._dispatch_update(*args))
self._updates_queue.add(task) self._updates_queue.add(task)
task.add_done_callback(lambda _: self._updates_queue.discard(task)) task.add_done_callback(lambda _: self._updates_queue.discard(task))
else: else:
self._updates_queue.put_nowait(args) self._updates_queue.put_nowait(args)
if not self._dispatching_updates_queue.is_set(): if not self._dispatching_updates_queue.is_set():
self._dispatching_updates_queue.set() self._dispatching_updates_queue.set()
self._loop.create_task(self._dispatch_queue_updates()) self.loop.create_task(self._dispatch_queue_updates())
self._state_cache.update(update) self._state_cache.update(update)