2018-02-09 18:41:23 +03:00
|
|
|
.. _update-modes:
|
|
|
|
|
|
|
|
============
|
|
|
|
Update Modes
|
|
|
|
============
|
|
|
|
|
2018-06-21 22:54:54 +03:00
|
|
|
With ``asyncio``, the library has several tasks running in the background.
|
|
|
|
One task is used for sending requests, another task is used to receive them,
|
|
|
|
and a third one is used to handle updates.
|
2018-02-09 18:41:23 +03:00
|
|
|
|
2018-06-21 22:54:54 +03:00
|
|
|
To handle updates, you must keep your script running. You can do this in
|
|
|
|
several ways. For instance, if you are *not* running ``asyncio``'s event
|
|
|
|
loop, you should use `client.run_until_disconnected
|
|
|
|
<telethon.client.updates.UpdateMethods.run_until_disconnected>`:
|
2018-02-09 18:41:23 +03:00
|
|
|
|
2018-06-21 22:54:54 +03:00
|
|
|
.. code-block:: python
|
2018-02-09 18:41:23 +03:00
|
|
|
|
2018-06-21 22:54:54 +03:00
|
|
|
import asyncio
|
|
|
|
from telethon import TelegramClient
|
2018-02-09 18:41:23 +03:00
|
|
|
|
2018-06-21 22:54:54 +03:00
|
|
|
client = TelegramClient(...)
|
|
|
|
...
|
|
|
|
client.run_until_disconnected()
|
2018-02-09 18:41:23 +03:00
|
|
|
|
|
|
|
|
2018-06-21 22:54:54 +03:00
|
|
|
Behind the scenes, this method is ``await``'ing on the `client.disconnected
|
2018-06-22 15:44:59 +03:00
|
|
|
<telethon.client.telegrambaseclient.TelegramBaseClient.disconnected>` property,
|
|
|
|
so the code above and the following are equivalent:
|
2018-02-09 18:41:23 +03:00
|
|
|
|
2018-06-21 22:54:54 +03:00
|
|
|
.. code-block:: python
|
2018-02-09 18:41:23 +03:00
|
|
|
|
2018-06-21 22:54:54 +03:00
|
|
|
import asyncio
|
|
|
|
from telethon import TelegramClient
|
2018-02-09 18:41:23 +03:00
|
|
|
|
2018-06-21 22:54:54 +03:00
|
|
|
client = TelegramClient(...)
|
2018-02-09 18:41:23 +03:00
|
|
|
|
2018-06-21 22:54:54 +03:00
|
|
|
async def main():
|
|
|
|
await client.disconnected
|
2018-02-09 18:41:23 +03:00
|
|
|
|
2018-06-25 22:14:58 +03:00
|
|
|
loop = asyncio.get_event_loop()
|
|
|
|
loop.run_until_complete(main())
|
2018-02-09 18:41:23 +03:00
|
|
|
|
|
|
|
|
2018-06-21 22:54:54 +03:00
|
|
|
You could also run `client.disconnected
|
2018-06-22 15:44:59 +03:00
|
|
|
<telethon.client.telegrambaseclient.TelegramBaseClient.disconnected>`
|
|
|
|
until it completed.
|
2018-02-09 18:41:23 +03:00
|
|
|
|
2018-06-21 22:54:54 +03:00
|
|
|
But if you don't want to ``await``, then you should know what you want
|
|
|
|
to be doing instead! What matters is that you shouldn't let your script
|
|
|
|
die. If you don't care about updates, you don't need any of this.
|
2018-06-25 22:14:58 +03:00
|
|
|
|
|
|
|
Notice that unlike `client.disconnected
|
|
|
|
<telethon.client.telegrambaseclient.TelegramBaseClient.disconnected>`,
|
|
|
|
`client.run_until_disconnected
|
|
|
|
<telethon.client.updates.UpdateMethods.run_until_disconnected>` will
|
|
|
|
handle ``KeyboardInterrupt`` with you. This method is special and can
|
|
|
|
also be ran while the loop is running, so you can do this:
|
|
|
|
|
|
|
|
.. code-block:: python
|
|
|
|
|
|
|
|
async def main():
|
|
|
|
await client.run_until_disconnected()
|
|
|
|
|
|
|
|
loop.run_until_complete(main())
|
2018-10-06 21:20:11 +03:00
|
|
|
|
|
|
|
|
|
|
|
If you need to process updates sequentially (i.e. not in parallel),
|
|
|
|
you should set ``sequential_updates=True`` when creating the client:
|
|
|
|
|
|
|
|
.. code-block:: python
|
|
|
|
|
|
|
|
with TelegramClient(..., sequential_updates=True) as client:
|
|
|
|
...
|