1
# Copyright (c) 2001-2004 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
Maintainer: U{Itamar Shtull-Trauring<mailto:twisted@itamarst.org>}
22
You're going to need to patch PyKqueue::
24
=====================================================
25
--- PyKQueue-1.3/kqsyscallmodule.c Sun Jan 28 21:59:50 2001
26
+++ PyKQueue-1.3/kqsyscallmodule.c.new Tue Jul 30 18:06:08 2002
30
statichere PyTypeObject KQEvent_Type = {
31
- PyObject_HEAD_INIT(NULL)
32
+ PyObject_HEAD_INIT(&PyType_Type)
35
sizeof(KQEventObject), // tp_basicsize
38
/* Build timespec for timeout */
39
totimespec.tv_sec = timeout / 1000;
40
- totimespec.tv_nsec = (timeout % 1000) * 100000;
41
+ totimespec.tv_nsec = (timeout % 1000) * 1000000;
43
// printf("timespec: sec=%d nsec=%d\\n", totimespec.tv_sec, totimespec.tv_nsec);
47
+ Py_BEGIN_ALLOW_THREADS
48
gotNumEvents = kevent (self->fd, changelist, haveNumEvents, triggered, wantNumEvents, &totimespec);
49
+ Py_END_ALLOW_THREADS
51
/* Don't need the input event list anymore, so get rid of it */
54
statichere PyTypeObject KQueue_Type = {
55
/* The ob_type field must be initialized in the module init function
56
* to be portable to Windows without using C++. */
57
- PyObject_HEAD_INIT(NULL)
58
+ PyObject_HEAD_INIT(&PyType_Type)
61
sizeof(KQueueObject), /*tp_basicsize*/
69
from kqsyscall import *
72
from twisted.python import log, failure
85
class KQueueReactor(posixbase.PosixReactorBase):
86
"""A reactor that uses kqueue(2)/kevent(2)."""
88
def _updateRegistration(self, *args):
89
kq.kevent([kevent(*args)], 0, 0)
91
def addReader(self, reader):
92
"""Add a FileDescriptor for notification of data available to read.
95
if not reads.has_key(fd):
96
selectables[fd] = reader
98
self._updateRegistration(fd, EVFILT_READ, EV_ADD)
100
def addWriter(self, writer, writes=writes, selectables=selectables):
101
"""Add a FileDescriptor for notification of data available to write.
104
if not writes.has_key(fd):
105
selectables[fd] = writer
107
self._updateRegistration(fd, EVFILT_WRITE, EV_ADD)
109
def removeReader(self, reader):
110
"""Remove a Selectable for notification of data available to read.
113
if reads.has_key(fd):
115
if not writes.has_key(fd): del selectables[fd]
116
self._updateRegistration(fd, EVFILT_READ, EV_DELETE)
118
def removeWriter(self, writer, writes=writes):
119
"""Remove a Selectable for notification of data available to write.
122
if writes.has_key(fd):
124
if not reads.has_key(fd): del selectables[fd]
125
self._updateRegistration(fd, EVFILT_WRITE, EV_DELETE)
128
"""Remove all selectables, and return a list of them."""
129
if self.waker is not None:
130
self.removeReader(self.waker)
131
result = selectables.values()
132
for fd in reads.keys():
133
self._updateRegistration(fd, EVFILT_READ, EV_DELETE)
134
for fd in writes.keys():
135
self._updateRegistration(fd, EVFILT_WRITE, EV_DELETE)
139
if self.waker is not None:
140
self.addReader(self.waker)
143
def doKEvent(self, timeout,
146
selectables=selectables,
150
EVFILT_READ=EVFILT_READ,
151
EVFILT_WRITE=EVFILT_WRITE):
152
"""Poll the kqueue for new events."""
156
timeout = int(timeout * 1000) # convert seconds to milliseconds
159
l = kq.kevent([], len(selectables), timeout)
161
if e[0] == errno.EINTR:
165
_drdw = self._doWriteOrRead
168
fd, filter = event.ident, event.filter
169
selectable = selectables[fd]
170
log.callWithLogger(selectable, _drdw, selectable, fd, filter)
172
def _doWriteOrRead(self, selectable, fd, filter):
174
if filter == EVFILT_READ:
175
why = selectable.doRead()
176
if filter == EVFILT_WRITE:
177
why = selectable.doWrite()
178
if not selectable.fileno() == fd:
179
why = main.CONNECTION_LOST
181
why = sys.exc_info()[1]
185
self.removeReader(selectable)
186
self.removeWriter(selectable)
187
selectable.connectionLost(failure.Failure(why))
189
doIteration = doKEvent
194
main.installReactor(k)
197
__all__ = ["KQueueReactor", "install"]