Encourage to .disconnect() when done

Lonami 2017-11-04 21:23:37 +01:00
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. * With several worker threads that run your update handlers.
* A mix of the above. * 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 ## Using multiple workers
@ -35,6 +35,7 @@ def replier(update):
client.add_update_handler(replier) client.add_update_handler(replier)
input('Press enter to stop this!') 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. 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 ```python
while True: while True:
update = client.updates.poll() try:
if not update: update = client.updates.poll()
continue 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`. 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.connect()
client.add_update_handler(callback) client.add_update_handler(callback)
client.idle() client.idle() # ends with Ctrl+C
client.disconnect()
``` ```