Fix workers not stopping on .disconnect(), start them on login

This commit is contained in:
Lonami Exo 2017-10-01 19:56:24 +02:00
parent 62ea77cbea
commit d98fd6a424
2 changed files with 8 additions and 5 deletions

View File

@ -267,6 +267,9 @@ class TelegramBareClient:
self._user_connected = False self._user_connected = False
self._recv_thread = None self._recv_thread = None
# Stop the workers from the background thread
self.updates.stop_workers()
# This will trigger a "ConnectionResetError", for subsequent calls # This will trigger a "ConnectionResetError", for subsequent calls
# to read or send (from another thread) and usually, the background # to read or send (from another thread) and usually, the background
# thread would try restarting the connection but since the # thread would try restarting the connection but since the
@ -747,6 +750,7 @@ class TelegramBareClient:
def _set_connected_and_authorized(self): def _set_connected_and_authorized(self):
self._authorized = True self._authorized = True
self.updates.setup_workers()
if self._spawn_read_thread and 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,

View File

@ -32,7 +32,6 @@ class UpdateState:
# https://core.telegram.org/api/updates # https://core.telegram.org/api/updates
self._state = tl.updates.State(0, 0, datetime.now(), 0, 0) self._state = tl.updates.State(0, 0, datetime.now(), 0, 0)
self._setup_workers()
def can_poll(self): def can_poll(self):
"""Returns True if a call to .poll() won't lock""" """Returns True if a call to .poll() won't lock"""
@ -66,16 +65,16 @@ class UpdateState:
"""Changes the number of workers running. """Changes the number of workers running.
If 'n is None', clears all pending updates from memory. If 'n is None', clears all pending updates from memory.
""" """
self._stop_workers() self.stop_workers()
self._workers = n self._workers = n
if n is None: if n is None:
self._updates.clear() self._updates.clear()
else: else:
self._setup_workers() self.setup_workers()
workers = property(fget=get_workers, fset=set_workers) workers = property(fget=get_workers, fset=set_workers)
def _stop_workers(self): def stop_workers(self):
"""Raises "StopIterationException" on the worker threads to stop them, """Raises "StopIterationException" on the worker threads to stop them,
and also clears all of them off the list and also clears all of them off the list
""" """
@ -93,7 +92,7 @@ class UpdateState:
self._worker_threads.clear() self._worker_threads.clear()
def _setup_workers(self): def setup_workers(self):
if self._worker_threads or not self._workers: if self._worker_threads or not self._workers:
# There already are workers, or workers is None or 0. Do nothing. # There already are workers, or workers is None or 0. Do nothing.
return return