Revert "Allow stop, restart and add/remove_handler to be non-blocking"

This reverts commit 8e9e8b4a
This commit is contained in:
Dan 2020-02-01 16:05:58 +01:00
parent ce93f0ac64
commit 51f88ef1bf
2 changed files with 38 additions and 86 deletions

View File

@ -328,18 +328,12 @@ class Client(Methods, BaseClient):
self.is_initialized = True
def terminate(self, block: bool = True):
def terminate(self):
"""Terminate the client by shutting down workers.
This method does the opposite of :meth:`~Client.initialize`.
It will stop the dispatcher and shut down updates and download workers.
Parameters:
block (``bool``, *optional*):
Blocks the code execution until the client has been terminated. It is useful with ``block=False`` in
case you want to terminate the own client *within* an handler in order not to cause a deadlock.
Defaults to True.
Raises:
ConnectionError: In case you try to terminate a client that is already terminated.
"""
@ -351,7 +345,7 @@ class Client(Methods, BaseClient):
log.warning("Takeout session {} finished".format(self.takeout_id))
Syncer.remove(self)
self.dispatcher.stop(block)
self.dispatcher.stop()
for _ in range(self.DOWNLOAD_WORKERS):
self.download_queue.put(None)
@ -846,17 +840,11 @@ class Client(Methods, BaseClient):
self.initialize()
return self
def stop(self, block: bool = True):
def stop(self):
"""Stop the Client.
This method disconnects the client from Telegram and stops the underlying tasks.
Parameters:
block (``bool``, *optional*):
Blocks the code execution until the client has been stopped. It is useful with ``block=False`` in case
you want to stop the own client *within* an handler in order not to cause a deadlock.
Defaults to True.
Returns:
:obj:`Client`: The stopped client itself.
@ -876,23 +864,17 @@ class Client(Methods, BaseClient):
app.stop()
"""
self.terminate(block)
self.terminate()
self.disconnect()
return self
def restart(self, block: bool = True):
def restart(self):
"""Restart the Client.
This method will first call :meth:`~Client.stop` and then :meth:`~Client.start` in a row in order to restart
a client using a single method.
Parameters:
block (``bool``, *optional*):
Blocks the code execution until the client has been restarted. It is useful with ``block=False`` in case
you want to restart the own client *within* an handler in order not to cause a deadlock.
Defaults to True.
Returns:
:obj:`Client`: The restarted client itself.
@ -916,7 +898,7 @@ class Client(Methods, BaseClient):
app.stop()
"""
self.stop(block)
self.stop()
self.start()
return self
@ -1003,7 +985,7 @@ class Client(Methods, BaseClient):
Client.idle()
self.stop()
def add_handler(self, handler: Handler, group: int = 0, block: bool = True):
def add_handler(self, handler: Handler, group: int = 0):
"""Register an update handler.
You can register multiple handlers, but at most one handler within a group will be used for a single update.
@ -1018,11 +1000,6 @@ class Client(Methods, BaseClient):
group (``int``, *optional*):
The group identifier, defaults to 0.
block (``bool``, *optional*):
Blocks the code execution until the handler has been added. It is useful with ``block=False`` in case
you want to register a new handler *within* another handler in order not to cause a deadlock.
Defaults to True.
Returns:
``tuple``: A tuple consisting of *(handler, group)*.
@ -1044,11 +1021,11 @@ class Client(Methods, BaseClient):
if isinstance(handler, DisconnectHandler):
self.disconnect_handler = handler.callback
else:
self.dispatcher.add_handler(handler, group, block)
self.dispatcher.add_handler(handler, group)
return handler, group
def remove_handler(self, handler: Handler, group: int = 0, block: bool = True):
def remove_handler(self, handler: Handler, group: int = 0):
"""Remove a previously-registered update handler.
Make sure to provide the right group where the handler was added in. You can use the return value of the
@ -1061,13 +1038,6 @@ class Client(Methods, BaseClient):
group (``int``, *optional*):
The group identifier, defaults to 0.
block (``bool``, *optional*):
Blocks the code execution until the handler has been removed. It is useful with ``block=False`` in case
you want to remove a previously registered handler *within* another handler in order not to cause a
deadlock.
Defaults to True.
Example:
.. code-block:: python
:emphasize-lines: 11
@ -1089,7 +1059,7 @@ class Client(Methods, BaseClient):
if isinstance(handler, DisconnectHandler):
self.disconnect_handler = None
else:
self.dispatcher.remove_handler(handler, group, block)
self.dispatcher.remove_handler(handler, group)
def stop_transmission(self):
"""Stop downloading or uploading a file.

View File

@ -118,8 +118,7 @@ class Dispatcher:
self.workers_list[-1].start()
def stop(self, block: bool = True):
def do_it():
def stop(self):
for _ in range(self.workers):
self.updates_queue.put(None)
@ -130,13 +129,7 @@ class Dispatcher:
self.locks_list.clear()
self.groups.clear()
if block:
do_it()
else:
Thread(target=do_it).start()
def add_handler(self, handler, group: int, block: bool = True):
def do_it():
def add_handler(self, handler, group: int):
for lock in self.locks_list:
lock.acquire()
@ -150,13 +143,7 @@ class Dispatcher:
for lock in self.locks_list:
lock.release()
if block:
do_it()
else:
Thread(target=do_it).start()
def remove_handler(self, handler, group: int, block: bool = True):
def do_it():
def remove_handler(self, handler, group: int):
for lock in self.locks_list:
lock.acquire()
@ -169,11 +156,6 @@ class Dispatcher:
for lock in self.locks_list:
lock.release()
if block:
do_it()
else:
Thread(target=do_it).start()
def update_worker(self, lock):
name = threading.current_thread().name
log.debug("{} started".format(name))