Allow disabling spawning a second thread

This commit is contained in:
Lonami Exo 2017-09-30 11:28:15 +02:00
parent a3ae56ca9e
commit 61033b2f56
2 changed files with 16 additions and 5 deletions

View File

@ -68,6 +68,7 @@ class TelegramBareClient:
connection_mode=ConnectionMode.TCP_FULL, connection_mode=ConnectionMode.TCP_FULL,
proxy=None, proxy=None,
update_workers=None, update_workers=None,
spawn_read_thread=True,
timeout=timedelta(seconds=5), timeout=timedelta(seconds=5),
**kwargs): **kwargs):
"""Refer to TelegramClient.__init__ for docs on this method""" """Refer to TelegramClient.__init__ for docs on this method"""
@ -131,7 +132,9 @@ class TelegramBareClient:
# Uploaded files cache so subsequent calls are instant # Uploaded files cache so subsequent calls are instant
self._upload_cache = {} self._upload_cache = {}
# Constantly read for results and updates from within the main client # Constantly read for results and updates from within the main client,
# if the user has left enabled such option.
self._spawn_read_thread = spawn_read_thread
self._recv_thread = None self._recv_thread = None
# Identifier of the main thread (the one that called .connect()). # Identifier of the main thread (the one that called .connect()).
@ -704,7 +707,7 @@ class TelegramBareClient:
def _set_connected_and_authorized(self): def _set_connected_and_authorized(self):
self._authorized = True self._authorized = True
if self._recv_thread is None: if self._spawn_read_thread and self._recv_thread is None:
self._recv_thread = threading.Thread( self._recv_thread = threading.Thread(
name='ReadThread', daemon=True, name='ReadThread', daemon=True,
target=self._recv_thread_impl target=self._recv_thread_impl

View File

@ -59,6 +59,7 @@ class TelegramClient(TelegramBareClient):
proxy=None, proxy=None,
update_workers=None, update_workers=None,
timeout=timedelta(seconds=5), timeout=timedelta(seconds=5),
spawn_read_thread=True,
**kwargs): **kwargs):
"""Initializes the Telegram client with the specified API ID and Hash. """Initializes the Telegram client with the specified API ID and Hash.
@ -77,9 +78,15 @@ class TelegramClient(TelegramBareClient):
> 0: 'update_workers' background threads will be spawned, any > 0: 'update_workers' background threads will be spawned, any
any of them will invoke all the self.updates.handlers. any of them will invoke all the self.updates.handlers.
Despite the value of 'process_updates', if you later call If 'spawn_read_thread', a background thread will be started once
'.add_update_handler(...)', updates will also be processed an authorized user has been logged in to Telegram to read items
and the update objects will be passed to the handlers you added. (such as updates and responses) from the network as soon as they
occur, which will speed things up.
If you don't want to spawn any additional threads, pending updates
will be read and processed accordingly after invoking a request
and not immediately. This is useful if you don't care about updates
at all and have set 'update_workers=None'.
If more named arguments are provided as **kwargs, they will be If more named arguments are provided as **kwargs, they will be
used to update the Session instance. Most common settings are: used to update the Session instance. Most common settings are:
@ -95,6 +102,7 @@ class TelegramClient(TelegramBareClient):
connection_mode=connection_mode, connection_mode=connection_mode,
proxy=proxy, proxy=proxy,
update_workers=update_workers, update_workers=update_workers,
spawn_read_thread=spawn_read_thread,
timeout=timeout timeout=timeout
) )