1 # vim: set encoding=utf-8 et sw=4 sts=4 :
6 Please see EventLoop class documentation for more info.
9 from select import poll, POLLIN, POLLPRI, POLLERR
11 __ALL__ = ('EventLoop')
14 r"""EventLoop(file[, handler]) -> EventLoop instance
16 This class implements a simple event loop based on select module.
17 It "listens" to activity a single 'file' object (a file, a pipe,
18 a socket, or even a simple file descriptor) and calls a 'handler'
19 function (or the handle() method if you prefer subclassing) every
20 time the file is ready for reading (or has an error).
22 This is a really simple example of usage using a hanlder callable:
25 >>> def handle(event_loop):
26 data = os.read(event_loop.fileno, 100)
27 os.write(1, 'Received message: %r\n' % data)
28 >>> p = EventLoop(0, handle)
31 In this example only one event is handled (see the 'once' argument
34 A more complex example, making a subclass and explicitly stopping
35 the loop, looks something like this:
37 >>> class Test(EventLoop):
39 >>> data = os.read(self.fileno, 100)
43 >>> os.write(1, 'Received message: %r\n' % data)
47 This example loops until the user enters a single "q", when stop()
48 is called and the event loop is exited.
51 def __init__(self, file, handler=None):
52 r"""Initialize the EventLoop object.
54 See EventLoop class documentation for more info.
59 self.handler = handler
61 def __register(self, file):
62 r"__register(file) -> None :: Register a new file for polling."
64 self.poll.register(self.fileno, POLLIN | POLLPRI | POLLERR)
66 def set_file(self, file):
67 r"""set_file(file) -> None :: New file object to be monitored
69 Unregister the previous file object being monitored and register
72 self.poll.unregister(self.fileno)
76 r"get_file() -> file object/int :: Get the current file object/fd."
79 file = property(get_file, set_file, doc='File object (or descriptor)')
82 r"get_fileno() -> int :: Get the current file descriptor"
83 if hasattr(self.file, 'fileno'):
84 return self.file.fileno()
87 fileno = property(get_fileno, doc='File descriptor (never a file object)')
90 r"""stop() -> None :: Stop the event loop.
92 The event loop will be interrupted as soon as the current handler
97 def loop(self, once=False):
98 r"""loop([once]) -> None :: Wait for events.
100 Wait for events and handle then when they arrive. If once is True,
101 then only 1 event is processed and then this method returns.
104 res = self.poll.poll()
105 if self.handler is not None:
109 if self._stop or once:
114 r"handle() -> None :: Abstract method to be overriden to handle events."
115 raise NotImplementedError
117 if __name__ == '__main__':
121 def handle(event_loop):
122 data = os.read(event_loop.fileno, 100)
123 os.write(1, 'Received message: %r\n' % data)
125 p = EventLoop(0, handle)
127 os.write(1, 'Say something once: ')
129 os.write(1, 'Great!\n')
131 class Test(EventLoop):
133 data = os.read(self.fileno, 100)
137 os.write(1, 'Received message: %r\n' % data)
141 os.write(1, 'Say a lot of things, then press write just "q" to stop: ')
143 os.write(1, 'Ok, bye!\n')