Add idle mechanism to gently stop the program

This commit is contained in:
Dan 2017-12-17 13:52:33 +01:00
parent c83daf8771
commit b6607fede0

View File

@ -26,6 +26,8 @@ import time
from collections import namedtuple from collections import namedtuple
from configparser import ConfigParser from configparser import ConfigParser
from hashlib import sha256, md5 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 import functions, types
from pyrogram.api.core import Object from pyrogram.api.core import Object
@ -71,6 +73,7 @@ class Client:
self.session = None self.session = None
self.update_handler = None self.update_handler = None
self.is_idle = Event()
# TODO: Better update handler # TODO: Better update handler
def set_update_handler(self, callback: callable): def set_update_handler(self, callback: callable):
@ -79,6 +82,16 @@ class Client:
def send(self, data: Object): def send(self, data: Object):
return self.session.send(data) 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): def authorize(self):
while True: while True:
phone_number = input("Enter phone number: ") phone_number = input("Enter phone number: ")