From: Leandro Lucarella Date: Mon, 24 Sep 2007 16:51:02 +0000 (-0300) Subject: Add a LoopInterruptedError to the EventLoop. X-Git-Url: https://git.llucax.com/software/pymin.git/commitdiff_plain/ac5053f5816193893e962448544daab87a975df2 Add a LoopInterruptedError to the EventLoop. This error is raised as a translation for the select.error, raised when the system call is interrupted by a signal, so EventLoop users don't have to know the details about the loop. --- diff --git a/eventloop.py b/eventloop.py index 7da492d..acc370a 100644 --- a/eventloop.py +++ b/eventloop.py @@ -6,10 +6,19 @@ A simple event loop. Please see EventLoop class documentation for more info. """ -from select import poll, POLLIN, POLLPRI, POLLERR +import select +from select import POLLIN, POLLPRI, POLLERR __ALL__ = ('EventLoop') +class LoopInterruptedError(RuntimeError): + def __init__(self, select_error): + self.select_error = select_error + def __repr__(self): + return 'LoopInterruptedError(select_error=%r)' % self.select_error + def __str__(self): + return 'Loop interrupted: %s' % self.select_error + class EventLoop: r"""EventLoop(file[, handler]) -> EventLoop instance @@ -53,7 +62,7 @@ class EventLoop: See EventLoop class documentation for more info. """ - self.poll = poll() + self.poll = select.poll() self._stop = False self.__register(file) self.handler = handler @@ -101,7 +110,10 @@ class EventLoop: then only 1 event is processed and then this method returns. """ while True: - res = self.poll.poll() + try: + res = self.poll.poll() + except select.error, e: + raise LoopInterruptedError(e) if self.handler is not None: self.handler(self) else: diff --git a/pymindaemon.py b/pymindaemon.py index 1868ae2..b7c64e4 100644 --- a/pymindaemon.py +++ b/pymindaemon.py @@ -11,7 +11,7 @@ command-line. import signal import socket from dispatcher import Dispatcher -from eventloop import EventLoop +from eventloop import EventLoop, LoopInterruptedError class PyminDaemon(EventLoop): r"""PyminDaemon(bind_addr, routes) -> PyminDaemon instance @@ -68,7 +68,10 @@ class PyminDaemon(EventLoop): def run(self): r"run() -> None :: Run the event loop (shortcut to loop())" - return self.loop() + try: + return self.loop() + except LoopInterruptedError, e: + pass if __name__ == '__main__':