Mention how not to spawn additional workers

Lonami 2017-10-22 13:28:53 +02:00
parent 71edb0930b
commit 19a30eea3b

@ -41,7 +41,25 @@ We only ask you one thing: don't keep this running for too long, or your contact
## Spawning no worker at all
**WIP**
All the workers do is loop forever and poll updates from a queue that is filled from the `ReadThread`, responsible for reading every item off the network. If you only need a worker and the `MainThread` would be doing no other job, this is the preferred way. You can easily do the same as the workers like so:
```python
while True:
update = client.updates.poll()
if not update:
continue
print('I received', 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`.
This can coexist with the rest of `N` workers, or you can set it to `0` additional workers:
```python
client = TelegramClient('session', api_id, api_hash, update_workers=0)
```
You **must** set it to `0` (or other number), as it defaults to `None` and there is a different. `None` workers means updates won't be processed *at all*, so you must set it to some value (0 or greater) if you want `client.updates.poll()` to work.
## Using the main thread instead the `ReadThread`
@ -65,7 +83,7 @@ As a complete example:
def callback(update):
print('I received', update)
client = TelegramClient('anon', api_id, api_hash,
client = TelegramClient('session', api_id, api_hash,
update_workers=1, spawn_read_thread=False)
client.connect()