From 017be3498a6237c10a50888d24e23ab8cc516434 Mon Sep 17 00:00:00 2001 From: Lonami Date: Sat, 4 Nov 2017 21:23:37 +0100 Subject: [PATCH] Encourage to .disconnect() when done --- Working-with-Updates.md | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/Working-with-Updates.md b/Working-with-Updates.md index 0ab8046..8ec8c68 100644 --- a/Working-with-Updates.md +++ b/Working-with-Updates.md @@ -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() ``` \ No newline at end of file