From b6607fede093f623f1a0f4eb157a9d86b9544577 Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Sun, 17 Dec 2017 13:52:33 +0100 Subject: [PATCH] Add idle mechanism to gently stop the program --- pyrogram/client/client.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/pyrogram/client/client.py b/pyrogram/client/client.py index dc0a6d55..8b2ac1cc 100644 --- a/pyrogram/client/client.py +++ b/pyrogram/client/client.py @@ -26,6 +26,8 @@ import time from collections import namedtuple from configparser import ConfigParser from hashlib import sha256, md5 +from signal import signal, SIGINT, SIGTERM, SIGABRT +from threading import Event from pyrogram.api import functions, types from pyrogram.api.core import Object @@ -71,6 +73,7 @@ class Client: self.session = None self.update_handler = None + self.is_idle = Event() # TODO: Better update handler def set_update_handler(self, callback: callable): @@ -79,6 +82,16 @@ class Client: def send(self, data: Object): return self.session.send(data) + def signal_handler(self, *args): + self.stop() + self.is_idle.set() + + def idle(self, stop_signals: tuple = (SIGINT, SIGTERM, SIGABRT)): + for s in stop_signals: + signal(s, self.signal_handler) + + self.is_idle.wait() + def authorize(self): while True: phone_number = input("Enter phone number: ")