1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
|
# Copyright (c) 2001-2007 Twisted Matrix Laboratories.
# See LICENSE for details.
"""
An epoll() based implementation of the twisted main loop.
To install the event loop (and you should do this before any connections,
listeners or connectors are added)::
from twisted.internet import epollreactor
epollreactor.install()
Maintainer: U{Jp Calderone <mailto:exarkun@twistedmatrix.com>}
"""
import sys, errno
from zope.interface import implements
from twisted.internet.interfaces import IReactorFDSet
from twisted.python import _epoll
from twisted.python import log
from twisted.internet import posixbase, error
from twisted.internet.main import CONNECTION_LOST
_POLL_DISCONNECTED = (_epoll.HUP | _epoll.ERR)
class EPollReactor(posixbase.PosixReactorBase):
"""
A reactor that uses epoll(4).
@ivar _poller: A L{poll} which will be used to check for I/O
readiness.
@ivar _selectables: A dictionary mapping integer file descriptors to
instances of L{FileDescriptor} which have been registered with the
reactor. All L{FileDescriptors} which are currently receiving read or
write readiness notifications will be present as values in this
dictionary.
@ivar _reads: A dictionary mapping integer file descriptors to arbitrary
values (this is essentially a set). Keys in this dictionary will be
registered with C{_poller} for read readiness notifications which will
be dispatched to the corresponding L{FileDescriptor} instances in
C{_selectables}.
@ivar _writes: A dictionary mapping integer file descriptors to arbitrary
values (this is essentially a set). Keys in this dictionary will be
registered with C{_poller} for write readiness notifications which will
be dispatched to the corresponding L{FileDescriptor} instances in
C{_selectables}.
"""
implements(IReactorFDSet)
def __init__(self):
"""
Initialize epoll object, file descriptor tracking dictionaries, and the
base class.
"""
# Create the poller we're going to use. The 1024 here is just a hint
# to the kernel, it is not a hard maximum.
self._poller = _epoll.epoll(1024)
self._reads = {}
self._writes = {}
self._selectables = {}
posixbase.PosixReactorBase.__init__(self)
def _add(self, xer, primary, other, selectables, event, antievent):
"""
Private method for adding a descriptor from the event loop.
It takes care of adding it if new or modifying it if already added
for another state (read -> read/write for example).
"""
fd = xer.fileno()
if fd not in primary:
cmd = _epoll.CTL_ADD
flags = event
if fd in other:
flags |= antievent
cmd = _epoll.CTL_MOD
primary[fd] = 1
selectables[fd] = xer
# epoll_ctl can raise all kinds of IOErrors, and every one
# indicates a bug either in the reactor or application-code.
# Let them all through so someone sees a traceback and fixes
# something. We'll do the same thing for every other call to
# this method in this file.
self._poller._control(cmd, fd, flags)
def addReader(self, reader):
"""
Add a FileDescriptor for notification of data available to read.
"""
self._add(reader, self._reads, self._writes, self._selectables, _epoll.IN, _epoll.OUT)
def addWriter(self, writer):
"""
Add a FileDescriptor for notification of data available to write.
"""
self._add(writer, self._writes, self._reads, self._selectables, _epoll.OUT, _epoll.IN)
def _remove(self, xer, primary, other, selectables, event, antievent):
"""
Private method for removing a descriptor from the event loop.
It does the inverse job of _add, and also add a check in case of the fd
has gone away.
"""
fd = xer.fileno()
if fd == -1:
for fd, fdes in selectables.items():
if xer is fdes:
break
else:
return
if fd in primary:
cmd = _epoll.CTL_DEL
flags = event
if fd in other:
flags = antievent
cmd = _epoll.CTL_MOD
else:
del selectables[fd]
del primary[fd]
# See comment above _control call in _add.
self._poller._control(cmd, fd, flags)
def removeReader(self, reader):
"""
Remove a Selectable for notification of data available to read.
"""
self._remove(reader, self._reads, self._writes, self._selectables, _epoll.IN, _epoll.OUT)
def removeWriter(self, writer):
"""
Remove a Selectable for notification of data available to write.
"""
self._remove(writer, self._writes, self._reads, self._selectables, _epoll.OUT, _epoll.IN)
def removeAll(self):
"""
Remove all selectables, and return a list of them.
"""
if self.waker is not None:
fd = self.waker.fileno()
if fd in self._reads:
del self._reads[fd]
del self._selectables[fd]
result = self._selectables.values()
fds = self._selectables.keys()
self._reads.clear()
self._writes.clear()
self._selectables.clear()
for fd in fds:
try:
# Actually, we'll ignore all errors from this, since it's
# just last-chance cleanup.
self._poller._control(_epoll.CTL_DEL, fd, 0)
except IOError:
pass
if self.waker is not None:
fd = self.waker.fileno()
self._reads[fd] = 1
self._selectables[fd] = self.waker
return result
def getReaders(self):
return [self._selectables[fd] for fd in self._reads]
def getWriters(self):
return [self._selectables[fd] for fd in self._writes]
def doPoll(self, timeout):
"""
Poll the poller for new events.
"""
if timeout is None:
timeout = 1
timeout = int(timeout * 1000) # convert seconds to milliseconds
try:
# Limit the number of events to the number of io objects we're
# currently tracking (because that's maybe a good heuristic) and
# the amount of time we block to the value specified by our
# caller.
l = self._poller.wait(len(self._selectables), timeout)
except IOError, err:
if err.errno == errno.EINTR:
return
# See epoll_wait(2) for documentation on the other conditions
# under which this can fail. They can only be due to a serious
# programming error on our part, so let's just announce them
# loudly.
raise
_drdw = self._doReadOrWrite
for fd, event in l:
try:
selectable = self._selectables[fd]
except KeyError:
pass
else:
log.callWithLogger(selectable, _drdw, selectable, fd, event)
doIteration = doPoll
def _doReadOrWrite(self, selectable, fd, event):
"""
fd is available for read or write, make the work and raise errors
if necessary.
"""
why = None
inRead = False
if event & _POLL_DISCONNECTED and not (event & _epoll.IN):
why = CONNECTION_LOST
else:
try:
if event & _epoll.IN:
why = selectable.doRead()
inRead = True
if not why and event & _epoll.OUT:
why = selectable.doWrite()
inRead = False
if selectable.fileno() != fd:
why = error.ConnectionFdescWentAway(
'Filedescriptor went away')
inRead = False
except:
log.err()
why = sys.exc_info()[1]
if why:
self._disconnectSelectable(selectable, why, inRead)
def install():
"""
Install the epoll() reactor.
"""
p = EPollReactor()
from twisted.internet.main import installReactor
installReactor(p)
__all__ = ["EPollReactor", "install"]
|