[Python-Dev] epoll() and kqueue wrapper for the select module
Christian Heimes
lists at cheimes.de
Thu Dec 20 18:08:47 CET 2007
Linux Kernel 2.6+ and BSD (including Mac OS X) have two I/O event
notification systems similar but superior to select() and poll(). From
the manuals:
kqueue, kevent -- kernel event notification mechanism
The kqueue() system call provides a generic method of notifying the user
when an event happens or a condition holds, based on the results of
small pieces of kernel code termed filters. A kevent is identified by
the (ident, filter) pair; there may only be one unique kevent per kqueue.
epoll - I/O event notification facility
epoll is a variant of poll(2) that can be used either as an
edge-triggered or a level-triggered interface and scales well to large
numbers of watched file descriptors.
I've written wrappers for both mechanisms. Both wrappers are inspired
from Twisted and select.poll()'s API. The interface is more Pythonic
than the available wrappers and it reduced the burden on the user. The
users don't have to deal with low level control file descriptors and the
fd is closed automatically when the object is collected.
epoll interface
>>> ep = select.epoll(1)
>>> ep.register(fd, select.EPOLL_IN | select.EPOLL_OUT)
>>> ep.modify(fd, select.EPOLL_OUT)
>>> events = ep.wait(1, 1000)
>>> ep.unregister(fd)
kqueue interface
The kqueue interface is more low level than the epoll interface. It has
too many options.
>>> kq = select.kqueue()
>>> ev = [select.kevent(fd, select.KQ_FILTER_WRITE,
select.KQ_EV_ONESHOT | select.KQ_EV_ADD)]
>>> kq.control(ev, 0, 0)
>>> events = kq.control([], 1, None)
I already talked to some Twisted guys and they really like it. A patch
is available at http://bugs.python.org/issue1657. The code needs more
unit tests and documentation updates.
Christian
More information about the Python-Dev
mailing list