Remove client.disconnected property

This commit is contained in:
Lonami Exo 2021-09-18 15:41:04 +02:00
parent bf61dd32af
commit e524a74b84
4 changed files with 18 additions and 43 deletions

View File

@ -394,6 +394,12 @@ However, most likely, you were already doing the right thing (or else you would'
"why is this not being edited", which you would most likely consider a bug rather than a feature). "why is this not being edited", which you would most likely consider a bug rather than a feature).
The client.disconnected property has been removed
-------------------------------------------------
``client.run_until_disconnected()`` should be used instead.
The TelegramClient is no longer made out of mixins The TelegramClient is no longer made out of mixins
-------------------------------------------------- --------------------------------------------------

View File

@ -301,9 +301,6 @@ def init(
def get_loop(self: 'TelegramClient') -> asyncio.AbstractEventLoop: def get_loop(self: 'TelegramClient') -> asyncio.AbstractEventLoop:
return asyncio.get_event_loop() return asyncio.get_event_loop()
def get_disconnected(self: 'TelegramClient') -> asyncio.Future:
return self._sender.disconnected
def get_flood_sleep_threshold(self): def get_flood_sleep_threshold(self):
return self._flood_sleep_threshold return self._flood_sleep_threshold

View File

@ -2823,22 +2823,6 @@ class TelegramClient:
""" """
return telegrambaseclient.get_loop(**locals()) return telegrambaseclient.get_loop(**locals())
@property
def disconnected(self: 'TelegramClient') -> asyncio.Future:
"""
Property with a ``Future`` that resolves upon disconnection.
Example
.. code-block:: python
# Wait for a disconnection to occur
try:
await client.disconnected
except OSError:
print('Error on disconnect')
"""
return telegrambaseclient.get_disconnected(**locals())
@property @property
def flood_sleep_threshold(self): def flood_sleep_threshold(self):
return telegrambaseclient.get_flood_sleep_threshold(**locals()) return telegrambaseclient.get_flood_sleep_threshold(**locals())
@ -2928,30 +2912,26 @@ class TelegramClient:
def run_until_disconnected(self: 'TelegramClient'): def run_until_disconnected(self: 'TelegramClient'):
""" """
Runs the event loop until the library is disconnected. Wait until the library is disconnected.
It also notifies Telegram that we want to receive updates It also notifies Telegram that we want to receive updates
as described in https://core.telegram.org/api/updates. as described in https://core.telegram.org/api/updates.
Event handlers will continue to run while the method awaits for a
disconnection to occur. Essentially, this method "blocks" until a
disconnection occurs, and keeps your code running if you have nothing
else to do.
Manual disconnections can be made by calling `disconnect() Manual disconnections can be made by calling `disconnect()
<telethon.client.telegrambaseclient.TelegramBaseClient.disconnect>` <telethon.client.telegrambaseclient.TelegramBaseClient.disconnect>`
or sending a ``KeyboardInterrupt`` (e.g. by pressing ``Ctrl+C`` on or exiting the context-manager using the client (for example, a
the console window running the script). ``KeyboardInterrupt`` by pressing ``Ctrl+C`` on the console window
would propagate the error, exit the ``with`` block and disconnect).
If a disconnection error occurs (i.e. the library fails to reconnect If a disconnection error occurs (i.e. the library fails to reconnect
automatically), said error will be raised through here, so you have a automatically), said error will be raised through here, so you have a
chance to ``except`` it on your own code. chance to ``except`` it on your own code.
If the loop is already running, this method returns a coroutine
that you should await on your own code.
.. note::
If you want to handle ``KeyboardInterrupt`` in your code,
simply run the event loop in your code too in any way, such as
``loop.run_forever()`` or ``await client.disconnected`` (e.g.
``loop.run_until_complete(client.disconnected)``).
Example Example
.. code-block:: python .. code-block:: python

View File

@ -18,23 +18,15 @@ if typing.TYPE_CHECKING:
Callback = typing.Callable[[typing.Any], typing.Any] Callback = typing.Callable[[typing.Any], typing.Any]
async def _run_until_disconnected(self: 'TelegramClient'):
try:
# Make a high-level request to notify that we want updates
await self(_tl.fn.updates.GetState())
return await self.disconnected
except KeyboardInterrupt:
pass
finally:
await self.disconnect()
async def set_receive_updates(self: 'TelegramClient', receive_updates): async def set_receive_updates(self: 'TelegramClient', receive_updates):
self._no_updates = not receive_updates self._no_updates = not receive_updates
if receive_updates: if receive_updates:
await self(_tl.fn.updates.GetState()) await self(_tl.fn.updates.GetState())
async def run_until_disconnected(self: 'TelegramClient'): async def run_until_disconnected(self: 'TelegramClient'):
return await _run_until_disconnected(self) # Make a high-level request to notify that we want updates
await self(_tl.fn.updates.GetState())
return await self._sender.disconnected
def on(self: 'TelegramClient', event: EventBuilder): def on(self: 'TelegramClient', event: EventBuilder):
def decorator(f): def decorator(f):