Mention how not to spawn the ReadThread

Lonami 2017-10-22 13:21:17 +02:00
parent 8a235d53bc
commit 71edb0930b

@ -43,6 +43,32 @@ We only ask you one thing: don't keep this running for too long, or your contact
**WIP** **WIP**
## Using the main thread instead the read thread ## Using the main thread instead the `ReadThread`
**WIP** 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 <kbd>Ctrl+C</kbd>, 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()
```