Remove UpdateState .set and .check error

This commit is contained in:
Lonami Exo 2017-09-30 18:39:31 +02:00
parent 933ae01d85
commit 8c3c990e74
2 changed files with 16 additions and 22 deletions

View File

@ -411,9 +411,6 @@ class TelegramBareClient:
The invoke will be retried up to 'retries' times before raising
ValueError().
"""
# Any error from a background thread will be "posted" and checked here
self.updates.check_error()
if not all(isinstance(x, TLObject) and
x.content_related for x in requests):
raise ValueError('You can only invoke requests, not types!')
@ -775,9 +772,16 @@ class TelegramBareClient:
while self._user_connected and not self._reconnect():
sleep(0.1) # Retry forever, this is instant messaging
except Exception as e:
except Exception as error:
# Unknown exception, pass it to the main thread
self.updates.set_error(e)
self._logger.debug(
'[ERROR] Unknown error on the read thread, please report',
error
)
# If something strange happens we don't want to enter an
# infinite loop where all we do is raise an exception, so
# add a little sleep to avoid the CPU usage going mad.
sleep(0.1)
break
self._recv_thread = None

View File

@ -55,7 +55,7 @@ class UpdateState:
self._updates_available.clear()
if isinstance(update, Exception):
raise update # Some error was set through .set_error()
raise update # Some error was set through (surely StopIteration)
return update
@ -79,7 +79,12 @@ class UpdateState:
"""Raises "StopIterationException" on the worker threads to stop them,
and also clears all of them off the list
"""
self.set_error(StopIteration())
with self._updates_lock:
# Insert at the beginning so the very next poll causes an error
# TODO Should this reset the pts and such?
self._updates.appendleft(StopIteration())
self._updates_available.set()
for t in self._worker_threads:
t.join()
@ -116,21 +121,6 @@ class UpdateState:
'[ERROR] Unhandled exception on worker {}'.format(wid), e
)
def set_error(self, error):
"""Sets an error, so that the next call to .poll() will raise it.
Can be (and is) used to pass exceptions between threads.
"""
with self._updates_lock:
# Insert at the beginning so the very next poll causes an error
# TODO Should this reset the pts and such?
self._updates.appendleft(error)
self._updates_available.set()
def check_error(self):
with self._updates_lock:
if self._updates and isinstance(self._updates[0], Exception):
raise self._updates.popleft()
def process(self, update):
"""Processes an update object. This method is normally called by
the library itself.