From f906b99347945d98973fb54a5606136ae9951674 Mon Sep 17 00:00:00 2001 From: Leandro Lucarella Date: Mon, 12 Nov 2007 23:42:59 -0300 Subject: [PATCH] Implement timer in PyminDaemon using EventLoop signal handling. --- pymin/pymindaemon.py | 33 ++++++++++++++++++++++----------- 1 file changed, 22 insertions(+), 11 deletions(-) diff --git a/pymin/pymindaemon.py b/pymin/pymindaemon.py index 8ed3f15..ccb943d 100644 --- a/pymin/pymindaemon.py +++ b/pymin/pymindaemon.py @@ -42,25 +42,32 @@ class PyminDaemon(eventloop.EventLoop): See PyminDaemon class documentation for more info. """ + # Timer timeout time + self.timer = timer # Create and bind socket sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) sock.bind(bind_addr) - # Create EventLoop - eventloop.EventLoop.__init__(self, sock, timer=timer) - # Create Dispatcher - #TODO root.pymin = PyminHandler() - self.dispatcher = dispatcher.Dispatcher(root) # Signal handling - def quit(signum, frame): + def quit(loop, signum): print "Shuting down ..." - self.stop() # tell main event loop to stop - def reload_config(signum, frame): + loop.stop() # tell main event loop to stop + def reload_config(loop, signum): print "Reloading configuration..." # TODO iterate handlers list propagating reload action - signal.signal(signal.SIGINT, quit) - signal.signal(signal.SIGTERM, quit) - signal.signal(signal.SIGUSR1, reload_config) + def timer(loop, signum): + loop.handle_timer() + signal.alarm(loop.timer) + # Create EventLoop + eventloop.EventLoop.__init__(self, sock, signals={ + signal.SIGINT: quit, + signal.SIGTERM: quit, + signal.SIGUSR1: reload_config, + signal.SIGALRM: timer, + }) + # Create Dispatcher + #TODO root.pymin = PyminHandler() + self.dispatcher = dispatcher.Dispatcher(root) def handle(self): r"handle() -> None :: Handle incoming events using the dispatcher." @@ -90,6 +97,10 @@ class PyminDaemon(eventloop.EventLoop): def run(self): r"run() -> None :: Run the event loop (shortcut to loop())" + # Start the timer + self.handle_timer() + signal.alarm(self.timer) + # Loop try: return self.loop() except eventloop.LoopInterruptedError, e: -- 2.43.0