2
0
mirror of https://github.com/pyrogram/pyrogram synced 2025-08-29 05:18:10 +00:00

Allow start/restart being used inside handlers with block=False

This commit is contained in:
Dan 2020-02-01 16:19:28 +01:00
parent 51f88ef1bf
commit d9cb9c59bf

View File

@ -840,11 +840,17 @@ class Client(Methods, BaseClient):
self.initialize()
return self
def stop(self):
def stop(self, block: bool = True):
"""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 restarted. 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.
@ -864,17 +870,29 @@ class Client(Methods, BaseClient):
app.stop()
"""
def do_it():
self.terminate()
self.disconnect()
if block:
do_it()
else:
Thread(target=do_it).start()
return self
def restart(self):
def restart(self, block: bool = True):
"""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.
@ -898,9 +916,15 @@ class Client(Methods, BaseClient):
app.stop()
"""
def do_it():
self.stop()
self.start()
if block:
do_it()
else:
Thread(target=do_it).start()
return self
@staticmethod