From 71edb0930bcdbf5c69d57c545dfd107d141fef52 Mon Sep 17 00:00:00 2001 From: Lonami Date: Sun, 22 Oct 2017 13:21:17 +0200 Subject: [PATCH] Mention how not to spawn the ReadThread --- Working-with-Updates.md | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/Working-with-Updates.md b/Working-with-Updates.md index f6f28ed..929c80d 100644 --- a/Working-with-Updates.md +++ b/Working-with-Updates.md @@ -43,6 +43,32 @@ We only ask you one thing: don't keep this running for too long, or your contact **WIP** -## Using the main thread instead the read thread +## Using the main thread instead the `ReadThread` -**WIP** \ No newline at end of file +If you have no work to do on the `MainThread` and you were planning to have a `while True: sleep(1)`, don't do that. Instead, don't spawn the secondary `ReadThread` at all like so: +```python +client = TelegramClient( + ... + spawn_read_thread=False +) +``` + +And then `.idle()` from the `MainThread`: +```python +client.idle() +``` + +You can stop it with Ctrl+C, and you can configure the signals to be used in a similar fashion to [Python Telegram Bot](https://github.com/python-telegram-bot/python-telegram-bot/blob/4b3315db6feebafb94edcaa803df52bb49999ced/telegram/ext/updater.py#L460). + +As a complete example: +```python +def callback(update): + print('I received', update) + +client = TelegramClient('anon', api_id, api_hash, + update_workers=1, spawn_read_thread=False) + +client.connect() +client.add_update_handler(callback) +client.idle() +``` \ No newline at end of file