Skip to content

Commit 2a38f6e

Browse files
Add clear_handlers parameter in stop/restart methods
1 parent 0396151 commit 2a38f6e

5 files changed

Lines changed: 24 additions & 8 deletions

File tree

pyrogram/dispatcher.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ async def start(self):
274274
if not self.client.skip_updates:
275275
await self.client.recover_gaps()
276276

277-
async def stop(self, clear: bool = True):
277+
async def stop(self, clear_handlers: bool = True):
278278
if callable(self.client.stop_handler):
279279
try:
280280
await self.client.stop_handler(self.client)
@@ -288,7 +288,7 @@ async def stop(self, clear: bool = True):
288288
for i in self.handler_worker_tasks:
289289
await i
290290

291-
if clear:
291+
if clear_handlers:
292292
self.handler_worker_tasks.clear()
293293
self.groups.clear()
294294

pyrogram/methods/auth/terminate.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,18 @@
2727
class Terminate:
2828
async def terminate(
2929
self: "pyrogram.Client",
30+
clear_handlers: bool = True
3031
):
3132
"""Terminate the client by shutting down workers.
3233
3334
This method does the opposite of :meth:`~pyrogram.Client.initialize`.
3435
It will stop the dispatcher and shut down updates and download workers.
3536
37+
Parameters:
38+
clear_handlers (``bool``, *optional*):
39+
Clear the already existing handlers on restart the client.
40+
Default to True.
41+
3642
Raises:
3743
ConnectionError: In case you try to terminate a client that is already terminated.
3844
"""
@@ -44,7 +50,7 @@ async def terminate(
4450
log.info("Takeout session %s finished", self.takeout_id)
4551

4652
await self.storage.save()
47-
await self.dispatcher.stop()
53+
await self.dispatcher.stop(clear_handlers=clear_handlers)
4854

4955
for media_session in self.media_sessions.values():
5056
await media_session.stop()

pyrogram/methods/utilities/restart.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222
class Restart:
2323
async def restart(
2424
self: "pyrogram.Client",
25-
block: bool = True
25+
block: bool = True,
26+
clear_handlers: bool = False
2627
):
2728
"""Restart the Client.
2829
@@ -35,6 +36,10 @@ async def restart(
3536
you want to restart the own client within an handler in order not to cause a deadlock.
3637
Defaults to True.
3738
39+
clear_handlers (``bool``, *optional*):
40+
Clear the already existing handlers on restart the client.
41+
Default to False.
42+
3843
Returns:
3944
:obj:`~pyrogram.Client`: The restarted client itself.
4045
@@ -62,7 +67,7 @@ async def main():
6267
"""
6368

6469
async def do_it():
65-
await self.stop()
70+
await self.stop(clear_handlers=clear_handlers)
6671
await self.start()
6772

6873
if block:

pyrogram/methods/utilities/stop.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222
class Stop:
2323
async def stop(
2424
self: "pyrogram.Client",
25-
block: bool = True
25+
block: bool = True,
26+
clear_handlers: bool = True
2627
):
2728
"""Stop the Client.
2829
@@ -34,6 +35,10 @@ async def stop(
3435
you want to stop the own client *within* a handler in order not to cause a deadlock.
3536
Defaults to True.
3637
38+
clear_handlers (``bool``, *optional*):
39+
Clear the already existing handlers on stop the client.
40+
Default to True.
41+
3742
Returns:
3843
:obj:`~pyrogram.Client`: The stopped client itself.
3944
@@ -60,7 +65,7 @@ async def main():
6065
"""
6166

6267
async def do_it():
63-
await self.terminate()
68+
await self.terminate(clear_handlers=clear_handlers)
6469
await self.disconnect()
6570

6671
if block:

pyrogram/qrlogin.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ async def raw_handler(client, update, users, chats):
8888
await asyncio.wait_for(event.wait(), timeout=timeout)
8989
finally:
9090
self.client.remove_handler(*handler)
91-
await self.client.dispatcher.stop(clear=False)
91+
await self.client.dispatcher.stop(clear_handlers=False)
9292

9393
r = await self.client.invoke(
9494
raw.functions.auth.ExportLoginToken(

0 commit comments

Comments
 (0)