1 # vim: set encoding=utf-8 et sw=4 sts=4 :
6 Please see EventLoop class documentation for more info.
10 from select import POLLIN, POLLPRI, POLLERR
12 __ALL__ = ('EventLoop')
14 class LoopInterruptedError(RuntimeError):
15 def __init__(self, select_error):
16 self.select_error = select_error
18 return 'LoopInterruptedError(select_error=%r)' % self.select_error
20 return 'Loop interrupted: %s' % self.select_error
23 r"""EventLoop(file[, handler]) -> EventLoop instance
25 This class implements a simple event loop based on select module.
26 It "listens" to activity a single 'file' object (a file, a pipe,
27 a socket, or even a simple file descriptor) and calls a 'handler'
28 function (or the handle() method if you prefer subclassing) every
29 time the file is ready for reading (or has an error).
31 This is a really simple example of usage using a hanlder callable:
34 >>> def handle(event_loop):
35 data = os.read(event_loop.fileno, 100)
36 os.write(1, 'Received message: %r\n' % data)
37 >>> p = EventLoop(0, handle)
40 In this example only one event is handled (see the 'once' argument
43 A more complex example, making a subclass and explicitly stopping
44 the loop, looks something like this:
46 >>> class Test(EventLoop):
48 >>> data = os.read(self.fileno, 100)
52 >>> os.write(1, 'Received message: %r\n' % data)
56 This example loops until the user enters a single "q", when stop()
57 is called and the event loop is exited.
60 def __init__(self, file, handler=None):
61 r"""Initialize the EventLoop object.
63 See EventLoop class documentation for more info.
65 self.poll = select.poll()
68 self.handler = handler
70 def __register(self, file):
71 r"__register(file) -> None :: Register a new file for polling."
73 self.poll.register(self.fileno, POLLIN | POLLPRI | POLLERR)
75 def set_file(self, file):
76 r"""set_file(file) -> None :: New file object to be monitored
78 Unregister the previous file object being monitored and register
81 self.poll.unregister(self.fileno)
85 r"get_file() -> file object/int :: Get the current file object/fd."
88 file = property(get_file, set_file, doc='File object (or descriptor)')
91 r"get_fileno() -> int :: Get the current file descriptor"
92 if hasattr(self.file, 'fileno'):
93 return self.file.fileno()
96 fileno = property(get_fileno, doc='File descriptor (never a file object)')
99 r"""stop() -> None :: Stop the event loop.
101 The event loop will be interrupted as soon as the current handler
106 def loop(self, once=False):
107 r"""loop([once]) -> None :: Wait for events.
109 Wait for events and handle then when they arrive. If once is True,
110 then only 1 event is processed and then this method returns.
114 res = self.poll.poll()
115 except select.error, e:
116 raise LoopInterruptedError(e)
117 if self.handler is not None:
121 if self._stop or once:
126 r"handle() -> None :: Abstract method to be overriden to handle events."
127 raise NotImplementedError
129 if __name__ == '__main__':
133 def handle(event_loop):
134 data = os.read(event_loop.fileno, 100)
135 os.write(1, 'Received message: %r\n' % data)
137 p = EventLoop(0, handle)
139 os.write(1, 'Say something once: ')
141 os.write(1, 'Great!\n')
143 class Test(EventLoop):
145 data = os.read(self.fileno, 100)
149 os.write(1, 'Received message: %r\n' % data)
153 os.write(1, 'Say a lot of things, then press write just "q" to stop: ')
155 os.write(1, 'Ok, bye!\n')