OLD | NEW |
| (Empty) |
1 # Copyright (c) 2001-2007 Twisted Matrix Laboratories. | |
2 # See LICENSE for details. | |
3 | |
4 | |
5 """ | |
6 A kqueue()/kevent() based implementation of the Twisted main loop. | |
7 | |
8 To install the event loop (and you should do this before any connections, | |
9 listeners or connectors are added):: | |
10 | |
11 | from twisted.internet import kqreactor | |
12 | kqreactor.install() | |
13 | |
14 This reactor only works on FreeBSD and requires PyKQueue 1.3, which is | |
15 available at: U{http://people.freebsd.org/~dwhite/PyKQueue/} | |
16 | |
17 Maintainer: U{Itamar Shtull-Trauring<mailto:twisted@itamarst.org>} | |
18 | |
19 | |
20 | |
21 You're going to need to patch PyKqueue:: | |
22 | |
23 ===================================================== | |
24 --- PyKQueue-1.3/kqsyscallmodule.c Sun Jan 28 21:59:50 2001 | |
25 +++ PyKQueue-1.3/kqsyscallmodule.c.new Tue Jul 30 18:06:08 2002 | |
26 @@ -137,7 +137,7 @@ | |
27 } | |
28 | |
29 statichere PyTypeObject KQEvent_Type = { | |
30 - PyObject_HEAD_INIT(NULL) | |
31 + PyObject_HEAD_INIT(&PyType_Type) | |
32 0, // ob_size | |
33 "KQEvent", // tp_name | |
34 sizeof(KQEventObject), // tp_basicsize | |
35 @@ -291,13 +291,14 @@ | |
36 | |
37 /* Build timespec for timeout */ | |
38 totimespec.tv_sec = timeout / 1000; | |
39 - totimespec.tv_nsec = (timeout % 1000) * 100000; | |
40 + totimespec.tv_nsec = (timeout % 1000) * 1000000; | |
41 | |
42 // printf("timespec: sec=%d nsec=%d\\n", totimespec.tv_sec, totimespec.tv
_nsec); | |
43 | |
44 /* Make the call */ | |
45 - | |
46 + Py_BEGIN_ALLOW_THREADS | |
47 gotNumEvents = kevent (self->fd, changelist, haveNumEvents, triggered, wa
ntNumEvents, &totimespec); | |
48 + Py_END_ALLOW_THREADS | |
49 | |
50 /* Don't need the input event list anymore, so get rid of it */ | |
51 free (changelist); | |
52 @@ -361,7 +362,7 @@ | |
53 statichere PyTypeObject KQueue_Type = { | |
54 /* The ob_type field must be initialized in the module init function | |
55 * to be portable to Windows without using C++. */ | |
56 - PyObject_HEAD_INIT(NULL) | |
57 + PyObject_HEAD_INIT(&PyType_Type) | |
58 0, /*ob_size*/ | |
59 "KQueue", /*tp_name*/ | |
60 sizeof(KQueueObject), /*tp_basicsize*/ | |
61 | |
62 """ | |
63 | |
64 import errno, sys | |
65 | |
66 from zope.interface import implements | |
67 | |
68 from kqsyscall import EVFILT_READ, EVFILT_WRITE, EV_DELETE, EV_ADD | |
69 from kqsyscall import kqueue, kevent | |
70 | |
71 from twisted.internet.interfaces import IReactorFDSet | |
72 | |
73 from twisted.python import log, failure | |
74 from twisted.internet import main, posixbase | |
75 | |
76 | |
77 class KQueueReactor(posixbase.PosixReactorBase): | |
78 """ | |
79 A reactor that uses kqueue(2)/kevent(2). | |
80 | |
81 @ivar _kq: A L{kqueue} which will be used to check for I/O readiness. | |
82 | |
83 @ivar _selectables: A dictionary mapping integer file descriptors to | |
84 instances of L{FileDescriptor} which have been registered with the | |
85 reactor. All L{FileDescriptors} which are currently receiving read or | |
86 write readiness notifications will be present as values in this | |
87 dictionary. | |
88 | |
89 @ivar _reads: A dictionary mapping integer file descriptors to arbitrary | |
90 values (this is essentially a set). Keys in this dictionary will be | |
91 registered with C{_kq} for read readiness notifications which will be | |
92 dispatched to the corresponding L{FileDescriptor} instances in | |
93 C{_selectables}. | |
94 | |
95 @ivar _writes: A dictionary mapping integer file descriptors to arbitrary | |
96 values (this is essentially a set). Keys in this dictionary will be | |
97 registered with C{_kq} for write readiness notifications which will be | |
98 dispatched to the corresponding L{FileDescriptor} instances in | |
99 C{_selectables}. | |
100 """ | |
101 implements(IReactorFDSet) | |
102 | |
103 def __init__(self): | |
104 """ | |
105 Initialize kqueue object, file descriptor tracking dictionaries, and the | |
106 base class. | |
107 """ | |
108 self._kq = kqueue() | |
109 self._reads = {} | |
110 self._writes = {} | |
111 self._selectables = {} | |
112 posixbase.PosixReactorBase.__init__(self) | |
113 | |
114 | |
115 def _updateRegistration(self, *args): | |
116 self._kq.kevent([kevent(*args)], 0, 0) | |
117 | |
118 def addReader(self, reader): | |
119 """Add a FileDescriptor for notification of data available to read. | |
120 """ | |
121 fd = reader.fileno() | |
122 if fd not in self._reads: | |
123 self._selectables[fd] = reader | |
124 self._reads[fd] = 1 | |
125 self._updateRegistration(fd, EVFILT_READ, EV_ADD) | |
126 | |
127 def addWriter(self, writer): | |
128 """Add a FileDescriptor for notification of data available to write. | |
129 """ | |
130 fd = writer.fileno() | |
131 if fd not in self._writes: | |
132 self._selectables[fd] = writer | |
133 self._writes[fd] = 1 | |
134 self._updateRegistration(fd, EVFILT_WRITE, EV_ADD) | |
135 | |
136 def removeReader(self, reader): | |
137 """Remove a Selectable for notification of data available to read. | |
138 """ | |
139 fd = reader.fileno() | |
140 if fd in self._reads: | |
141 del self._reads[fd] | |
142 if fd not in self._writes: | |
143 del self._selectables[fd] | |
144 self._updateRegistration(fd, EVFILT_READ, EV_DELETE) | |
145 | |
146 def removeWriter(self, writer): | |
147 """Remove a Selectable for notification of data available to write. | |
148 """ | |
149 fd = writer.fileno() | |
150 if fd in self._writes: | |
151 del self._writes[fd] | |
152 if fd not in self._reads: | |
153 del self._selectables[fd] | |
154 self._updateRegistration(fd, EVFILT_WRITE, EV_DELETE) | |
155 | |
156 def removeAll(self): | |
157 """Remove all selectables, and return a list of them.""" | |
158 if self.waker is not None: | |
159 self.removeReader(self.waker) | |
160 result = self._selectables.values() | |
161 for fd in self._reads.keys(): | |
162 self._updateRegistration(fd, EVFILT_READ, EV_DELETE) | |
163 for fd in self._writes.keys(): | |
164 self._updateRegistration(fd, EVFILT_WRITE, EV_DELETE) | |
165 self._reads.clear() | |
166 self._writes.clear() | |
167 self._selectables.clear() | |
168 if self.waker is not None: | |
169 self.addReader(self.waker) | |
170 return result | |
171 | |
172 | |
173 def getReaders(self): | |
174 return [self._selectables[fd] for fd in self._reads] | |
175 | |
176 | |
177 def getWriters(self): | |
178 return [self._selectables[fd] for fd in self._writes] | |
179 | |
180 | |
181 def doKEvent(self, timeout): | |
182 """Poll the kqueue for new events.""" | |
183 if timeout is None: | |
184 timeout = 1000 | |
185 else: | |
186 timeout = int(timeout * 1000) # convert seconds to milliseconds | |
187 | |
188 try: | |
189 l = self._kq.kevent([], len(self._selectables), timeout) | |
190 except OSError, e: | |
191 if e[0] == errno.EINTR: | |
192 return | |
193 else: | |
194 raise | |
195 _drdw = self._doWriteOrRead | |
196 for event in l: | |
197 why = None | |
198 fd, filter = event.ident, event.filter | |
199 try: | |
200 selectable = self._selectables[fd] | |
201 except KeyError: | |
202 # Handles the infrequent case where one selectable's | |
203 # handler disconnects another. | |
204 continue | |
205 log.callWithLogger(selectable, _drdw, selectable, fd, filter) | |
206 | |
207 def _doWriteOrRead(self, selectable, fd, filter): | |
208 try: | |
209 if filter == EVFILT_READ: | |
210 why = selectable.doRead() | |
211 if filter == EVFILT_WRITE: | |
212 why = selectable.doWrite() | |
213 if not selectable.fileno() == fd: | |
214 why = main.CONNECTION_LOST | |
215 except: | |
216 why = sys.exc_info()[1] | |
217 log.deferr() | |
218 | |
219 if why: | |
220 self.removeReader(selectable) | |
221 self.removeWriter(selectable) | |
222 selectable.connectionLost(failure.Failure(why)) | |
223 | |
224 doIteration = doKEvent | |
225 | |
226 | |
227 def install(): | |
228 k = KQueueReactor() | |
229 main.installReactor(k) | |
230 | |
231 | |
232 __all__ = ["KQueueReactor", "install"] | |
OLD | NEW |