1
# Copyright (c) 2001-2009 Twisted Matrix Laboratories.
2
# See LICENSE for details.
5
A kqueue()/kevent() based implementation of the Twisted main loop.
7
To install the event loop (and you should do this before any connections,
8
listeners or connectors are added)::
10
| from twisted.internet import kqreactor
13
This reactor only works on FreeBSD and requires PyKQueue 1.3, which is
14
available at: U{http://people.freebsd.org/~dwhite/PyKQueue/}
18
You're going to need to patch PyKqueue::
20
=====================================================
21
--- PyKQueue-1.3/kqsyscallmodule.c Sun Jan 28 21:59:50 2001
22
+++ PyKQueue-1.3/kqsyscallmodule.c.new Tue Jul 30 18:06:08 2002
26
statichere PyTypeObject KQEvent_Type = {
27
- PyObject_HEAD_INIT(NULL)
28
+ PyObject_HEAD_INIT(&PyType_Type)
31
sizeof(KQEventObject), // tp_basicsize
34
/* Build timespec for timeout */
35
totimespec.tv_sec = timeout / 1000;
36
- totimespec.tv_nsec = (timeout % 1000) * 100000;
37
+ totimespec.tv_nsec = (timeout % 1000) * 1000000;
39
// printf("timespec: sec=%d nsec=%d\\n", totimespec.tv_sec, totimespec.tv_nsec);
43
+ Py_BEGIN_ALLOW_THREADS
44
gotNumEvents = kevent (self->fd, changelist, haveNumEvents, triggered, wantNumEvents, &totimespec);
45
+ Py_END_ALLOW_THREADS
47
/* Don't need the input event list anymore, so get rid of it */
50
statichere PyTypeObject KQueue_Type = {
51
/* The ob_type field must be initialized in the module init function
52
* to be portable to Windows without using C++. */
53
- PyObject_HEAD_INIT(NULL)
54
+ PyObject_HEAD_INIT(&PyType_Type)
57
sizeof(KQueueObject), /*tp_basicsize*/
63
from zope.interface import implements
65
from kqsyscall import EVFILT_READ, EVFILT_WRITE, EV_DELETE, EV_ADD
66
from kqsyscall import kqueue, kevent
68
from twisted.internet.interfaces import IReactorFDSet
70
from twisted.python import log, failure
71
from twisted.internet import main, posixbase
74
class KQueueReactor(posixbase.PosixReactorBase):
76
A reactor that uses kqueue(2)/kevent(2).
78
@ivar _kq: A L{kqueue} which will be used to check for I/O readiness.
80
@ivar _selectables: A dictionary mapping integer file descriptors to
81
instances of L{FileDescriptor} which have been registered with the
82
reactor. All L{FileDescriptors} which are currently receiving read or
83
write readiness notifications will be present as values in this
86
@ivar _reads: A dictionary mapping integer file descriptors to arbitrary
87
values (this is essentially a set). Keys in this dictionary will be
88
registered with C{_kq} for read readiness notifications which will be
89
dispatched to the corresponding L{FileDescriptor} instances in
92
@ivar _writes: A dictionary mapping integer file descriptors to arbitrary
93
values (this is essentially a set). Keys in this dictionary will be
94
registered with C{_kq} for write readiness notifications which will be
95
dispatched to the corresponding L{FileDescriptor} instances in
98
implements(IReactorFDSet)
102
Initialize kqueue object, file descriptor tracking dictionaries, and the
108
self._selectables = {}
109
posixbase.PosixReactorBase.__init__(self)
112
def _updateRegistration(self, *args):
113
self._kq.kevent([kevent(*args)], 0, 0)
115
def addReader(self, reader):
116
"""Add a FileDescriptor for notification of data available to read.
119
if fd not in self._reads:
120
self._selectables[fd] = reader
122
self._updateRegistration(fd, EVFILT_READ, EV_ADD)
124
def addWriter(self, writer):
125
"""Add a FileDescriptor for notification of data available to write.
128
if fd not in self._writes:
129
self._selectables[fd] = writer
131
self._updateRegistration(fd, EVFILT_WRITE, EV_ADD)
133
def removeReader(self, reader):
134
"""Remove a Selectable for notification of data available to read.
137
if fd in self._reads:
139
if fd not in self._writes:
140
del self._selectables[fd]
141
self._updateRegistration(fd, EVFILT_READ, EV_DELETE)
143
def removeWriter(self, writer):
144
"""Remove a Selectable for notification of data available to write.
147
if fd in self._writes:
149
if fd not in self._reads:
150
del self._selectables[fd]
151
self._updateRegistration(fd, EVFILT_WRITE, EV_DELETE)
155
Remove all selectables, and return a list of them.
157
return self._removeAll(
158
[self._selectables[fd] for fd in self._reads],
159
[self._selectables[fd] for fd in self._writes])
162
def getReaders(self):
163
return [self._selectables[fd] for fd in self._reads]
166
def getWriters(self):
167
return [self._selectables[fd] for fd in self._writes]
170
def doKEvent(self, timeout):
171
"""Poll the kqueue for new events."""
175
timeout = int(timeout * 1000) # convert seconds to milliseconds
178
l = self._kq.kevent([], len(self._selectables), timeout)
180
if e[0] == errno.EINTR:
184
_drdw = self._doWriteOrRead
187
fd, filter = event.ident, event.filter
189
selectable = self._selectables[fd]
191
# Handles the infrequent case where one selectable's
192
# handler disconnects another.
194
log.callWithLogger(selectable, _drdw, selectable, fd, filter)
196
def _doWriteOrRead(self, selectable, fd, filter):
198
if filter == EVFILT_READ:
199
why = selectable.doRead()
200
if filter == EVFILT_WRITE:
201
why = selectable.doWrite()
202
if not selectable.fileno() == fd:
203
why = main.CONNECTION_LOST
205
why = sys.exc_info()[1]
209
self.removeReader(selectable)
210
self.removeWriter(selectable)
211
selectable.connectionLost(failure.Failure(why))
213
doIteration = doKEvent
218
main.installReactor(k)
221
__all__ = ["KQueueReactor", "install"]