X-Git-Url: https://git.llucax.com/software/pymin.git/blobdiff_plain/45d91d1b75b9aed9abbbae97ff962ab08f215177..f3eaf59b7f33ce2ab39f853834460e92d2e1ca76:/pymin/eventloop.py diff --git a/pymin/eventloop.py b/pymin/eventloop.py index fe3c4ec..5e4bec3 100644 --- a/pymin/eventloop.py +++ b/pymin/eventloop.py @@ -10,8 +10,9 @@ import select import errno import signal from select import POLLIN, POLLPRI, POLLERR +import logging ; log = logging.getLogger('pymin.eventloop') -__ALL__ = ('EventLoop', 'LoopInterruptedError') +__all__ = ('EventLoop', 'LoopInterruptedError') class LoopInterruptedError(RuntimeError): r""" @@ -95,6 +96,7 @@ class EventLoop: See EventLoop class documentation for more info. """ + log.debug(u'EventLoop(%r, %r, %r)', file, handler, signals) self.poll = select.poll() self._stop = False self.__register(file) @@ -156,6 +158,7 @@ class EventLoop: The event loop will be interrupted as soon as the current handler finishes. """ + log.debug(u'EventLoop.stop()') self._stop = True def loop(self, once=False): @@ -164,10 +167,12 @@ class EventLoop: Wait for events and handle then when they arrive. If once is True, then only 1 event is processed and then this method returns. """ + log.debug(u'EventLoop.loop(%s)', once) # List of pending signals global signals while True: try: + log.debug(u'EventLoop.loop: polling') res = self.poll.poll() except select.error, e: # The error is not an interrupt caused by a signal, then raise @@ -176,13 +181,16 @@ class EventLoop: # If we have signals to process, we just do it have_signals = bool(signals) while signals: - self.handle_signal(signals.pop(0)) + signum = signals.pop(0) + log.debug(u'EventLoop.loop: processing signal %d...', signum) + self.handle_signal(signum) # No signals to process, execute the regular handler if not have_signals: + log.debug(u'EventLoop.loop: processing event...') self.handle() - import os # Look if we have to stop if self._stop or once: + log.debug(u'EventLoop.loop: stopped') self._stop = False break @@ -196,6 +204,12 @@ class EventLoop: if __name__ == '__main__': + logging.basicConfig( + level = logging.DEBUG, + format = '%(asctime)s %(levelname)-8s %(message)s', + datefmt = '%H:%M:%S', + ) + import os import time @@ -205,7 +219,7 @@ if __name__ == '__main__': p = EventLoop(0, handle) - os.write(1, 'Say something once: ') + os.write(1, 'Say something once:\n') p.loop(once=True) os.write(1, 'Great!\n')