import errno
import signal
from select import POLLIN, POLLPRI, POLLERR
+import logging ; log = logging.getLogger('pymin.eventloop')
__ALL__ = ('EventLoop', 'LoopInterruptedError')
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)
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):
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
# 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
if __name__ == '__main__':
+ logging.basicConfig(
+ level = logging.DEBUG,
+ format = '%(asctime)s %(levelname)-8s %(message)s',
+ datefmt = '%H:%M:%S',
+ )
+
import os
import time
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')