mirror of
https://github.com/LonamiWebs/Telethon.git
synced 2024-11-28 12:23:45 +03:00
Encourage to .disconnect() when done
parent
fd0632c612
commit
017be3498a
|
@ -4,7 +4,7 @@ The library can run in four distinguishable modes:
|
|||
* With several worker threads that run your update handlers.
|
||||
* A mix of the above.
|
||||
|
||||
Since this section is about updates, we'll describe the simplest way to work with them.
|
||||
Since this section is about updates, we'll describe the simplest way to work with them. Remember that you should *always* call `client.disconnect()` once you're done.
|
||||
|
||||
## Using multiple workers
|
||||
|
||||
|
@ -35,6 +35,7 @@ def replier(update):
|
|||
|
||||
client.add_update_handler(replier)
|
||||
input('Press enter to stop this!')
|
||||
client.disconnect()
|
||||
```
|
||||
|
||||
We only ask you one thing: don't keep this running for too long, or your contacts will go mad.
|
||||
|
@ -45,11 +46,16 @@ All the workers do is loop forever and poll updates from a queue that is filled
|
|||
|
||||
```python
|
||||
while True:
|
||||
update = client.updates.poll()
|
||||
if not update:
|
||||
continue
|
||||
try:
|
||||
update = client.updates.poll()
|
||||
if not update:
|
||||
continue
|
||||
|
||||
print('I received', update)
|
||||
print('I received', update)
|
||||
except KeyboardIterrupt:
|
||||
break
|
||||
|
||||
client.disconnect()
|
||||
```
|
||||
|
||||
Note that `poll` accepts a `timeout=` parameter, and it will return `None` if other thread got the update before you could or if the timeout expired, so it's important to check `if not update`.
|
||||
|
@ -88,5 +94,6 @@ client = TelegramClient('session', api_id, api_hash,
|
|||
|
||||
client.connect()
|
||||
client.add_update_handler(callback)
|
||||
client.idle()
|
||||
client.idle() # ends with Ctrl+C
|
||||
client.disconnect()
|
||||
```
|
Loading…
Reference in New Issue
Block a user