-
Notifications
You must be signed in to change notification settings - Fork 0
/
monkeypatch.py
312 lines (273 loc) · 8.7 KB
/
monkeypatch.py
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
#!/usr/bin/env python3
#
# vim: set tabstop=4:
#
# Copyright (c) 2015 David Jander
#
# Based on code contained in the Python standard library:
# Copyright (c) 2001-2015 Python Software Foundation.
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This module does monkey-patching to add EPOLLPRI/EPOLLERR support to the
# asyncio module of python 3.4. Hopefully one day this will go into the
# python-tulip project.
# For this to work, this module needs to be imported before asyncio and/or
# selectors. Ideally this will be the first module imported by any main
# application.
#
import selectors
import select
import math
from selectors import EVENT_READ, EVENT_WRITE
EVENT_EXCEPT = (1 << 2)
selectors.EVENT_EXCEPT = EVENT_EXCEPT
#
# Patch SelectSelector
#
def SelectSelector___init__(self):
super().__init__()
self._readers = set()
self._writers = set()
self._excepter = set()
def SelectSelector_register(self, fileobj, events, data=None):
if (not events) or (events & ~(EVENT_READ | EVENT_WRITE | EVENT_EXCEPT)):
raise ValueError("Invalid events: {!r}".format(events))
key = selectors.SelectorKey(fileobj, self._fileobj_lookup(fileobj), events, data)
if key.fd in self._fd_to_key:
raise KeyError("{!r} (FD {}) is already registered"
.format(fileobj, key.fd))
self._fd_to_key[key.fd] = key
if events & EVENT_READ:
self._readers.add(key.fd)
if events & EVENT_WRITE:
self._writers.add(key.fd)
if events & EVENT_EXCEPT:
self._excepter.add(key.fd)
return key
def SelectSelector_unregister(self, fileobj):
key = super().unregister(fileobj)
self._readers.discard(key.fd)
self._writers.discard(key.fd)
self._excepters.discard(key.fd)
return key
def SelectSelector_select(self, timeout=None):
timeout = None if timeout is None else max(timeout, 0)
ready = []
try:
r, w, e = self._select(self._readers, self._writers, self._excepters, timeout)
except InterruptedError:
return ready
r = set(r)
w = set(w)
e = set(e)
for fd in r | w | e:
events = 0
if fd in r:
events |= EVENT_READ
if fd in w:
events |= EVENT_WRITE
if fd in e:
events |= EVENT_EXCEPT
key = self._key_from_fd(fd)
if key:
ready.append((key, events & key.events))
return ready
selectors.SelectSelector.__init__ = SelectSelector___init__
selectors.SelectSelector.register = SelectSelector_register
selectors.SelectSelector.unregister = SelectSelector_unregister
selectors.SelectSelector.select = SelectSelector_select
#
# Patch PollSelector
#
# TODO!!!
#
# Patch EpollSelector
#
def EpollSelector_register(self, fileobj, events, data=None):
if (not events) or (events & ~(EVENT_READ | EVENT_WRITE | EVENT_EXCEPT)):
raise ValueError("Invalid events: {!r}".format(events))
key = selectors.SelectorKey(fileobj, self._fileobj_lookup(fileobj), events, data)
if key.fd in self._fd_to_key:
raise KeyError("{!r} (FD {}) is already registered"
.format(fileobj, key.fd))
self._fd_to_key[key.fd] = key
epoll_events = 0
if events & EVENT_READ:
epoll_events |= select.EPOLLIN
if events & EVENT_WRITE:
epoll_events |= select.EPOLLOUT
if events & EVENT_EXCEPT:
epoll_events |= select.EPOLLPRI | select.EPOLLERR
self._epoll.register(key.fd, epoll_events)
return key
def EpollSelector_select(self, timeout=None):
if timeout is None:
timeout = -1
elif timeout <= 0:
timeout = 0
else:
# epoll_wait() has a resolution of 1 millisecond, round away
# from zero to wait *at least* timeout seconds.
timeout = math.ceil(timeout * 1e3) * 1e-3
max_ev = len(self._fd_to_key)
ready = []
try:
fd_event_list = self._epoll.poll(timeout, max_ev)
except InterruptedError:
return ready
for fd, event in fd_event_list:
events = 0
if event & select.EPOLLOUT:
events |= EVENT_WRITE
if event & select.EPOLLIN:
events |= EVENT_READ
if event & (select.EPOLLPRI | select.EPOLLERR):
events |= EVENT_EXCEPT
key = self._key_from_fd(fd)
if key:
ready.append((key, events & key.events))
return ready
selectors.EpollSelector.register = EpollSelector_register
selectors.EpollSelector.select = EpollSelector_select
from asyncio import selector_events, events
#
# Patch BaseSelectorEventLoop
#
def BaseSelectorEventLoop_add_reader(self, fd, callback, *args):
"""Add a reader callback."""
if self._selector is None:
raise RuntimeError('Event loop is closed')
handle = events.Handle(callback, args, self)
try:
key = self._selector.get_key(fd)
except KeyError:
self._selector.register(fd, selectors.EVENT_READ,
(handle, None, None))
else:
mask, (reader, writer, excepter) = key.events, key.data
self._selector.modify(fd, mask | selectors.EVENT_READ,
(handle, writer, excepter))
if reader is not None:
reader.cancel()
def BaseSelectorEventLoop_remove_reader(self, fd):
"""Remove a reader callback."""
if self._selector is None:
return False
try:
key = self._selector.get_key(fd)
except KeyError:
return False
else:
mask, (reader, writer, excepter) = key.events, key.data
mask &= ~selectors.EVENT_READ
if not mask:
self._selector.unregister(fd)
else:
self._selector.modify(fd, mask, (None, writer, excepter))
if reader is not None:
reader.cancel()
return True
else:
return False
def BaseSelectorEventLoop_add_writer(self, fd, callback, *args):
"""Add a writer callback.."""
if self._selector is None:
raise RuntimeError('Event loop is closed')
handle = events.Handle(callback, args, self)
try:
key = self._selector.get_key(fd)
except KeyError:
self._selector.register(fd, selectors.EVENT_WRITE,
(None, handle, None))
else:
mask, (reader, writer, excepter) = key.events, key.data
self._selector.modify(fd, mask | selectors.EVENT_WRITE,
(reader, handle, excepter))
if writer is not None:
writer.cancel()
def BaseSelectorEventLoop_remove_writer(self, fd):
"""Remove a writer callback."""
if self._selector is None:
return False
try:
key = self._selector.get_key(fd)
except KeyError:
return False
else:
mask, (reader, writer, excepter) = key.events, key.data
# Remove both writer and connector.
mask &= ~selectors.EVENT_WRITE
if not mask:
self._selector.unregister(fd)
else:
self._selector.modify(fd, mask, (reader, None, excepter))
if writer is not None:
writer.cancel()
return True
else:
return False
def BaseSelectorEventLoop_add_excepter(self, fd, callback, *args):
"""Add an excepter callback."""
if self._selector is None:
raise RuntimeError('Event loop is closed')
handle = events.Handle(callback, args, self)
try:
key = self._selector.get_key(fd)
except KeyError:
self._selector.register(fd, selectors.EVENT_EXCEPT,
(None, None, handle))
else:
mask, (reader, writer, excepter) = key.events, key.data
self._selector.modify(fd, mask | selectors.EVENT_EXCEPT,
(reader, writer, handle))
if excepter is not None:
excepter.cancel()
def BaseSelectorEventLoop_remove_excepter(self, fd):
"""Remove an excepter callback."""
if self._selector is None:
return False
try:
key = self._selector.get_key(fd)
except KeyError:
return False
else:
mask, (reader, writer, excepter) = key.events, key.data
mask &= ~selectors.EVENT_EXCEPT
if not mask:
self._selector.unregister(fd)
else:
self._selector.modify(fd, mask, (reader, writer, None))
if excepter is not None:
excepter.cancel()
return True
else:
return False
def BaseSelectorEventLoop__process_events(self, event_list):
for key, mask in event_list:
fileobj, (reader, writer, excepter) = key.fileobj, key.data
if mask & selectors.EVENT_READ and reader is not None:
if reader._cancelled:
self.remove_reader(fileobj)
else:
self._add_callback(reader)
if mask & selectors.EVENT_WRITE and writer is not None:
if writer._cancelled:
self.remove_writer(fileobj)
else:
self._add_callback(writer)
if mask & selectors.EVENT_EXCEPT and excepter is not None:
if excepter._cancelled:
self.remove_excepter(fileobj)
else:
self._add_callback(excepter)
selector_events.BaseSelectorEventLoop.add_reader = BaseSelectorEventLoop_add_reader
selector_events.BaseSelectorEventLoop.remove_reader = BaseSelectorEventLoop_remove_reader
selector_events.BaseSelectorEventLoop.add_writer = BaseSelectorEventLoop_add_writer
selector_events.BaseSelectorEventLoop.remove_writer = BaseSelectorEventLoop_remove_writer
selector_events.BaseSelectorEventLoop.add_excepter = BaseSelectorEventLoop_add_excepter
selector_events.BaseSelectorEventLoop.remove_excepter = BaseSelectorEventLoop_remove_excepter
selector_events.BaseSelectorEventLoop._process_events = BaseSelectorEventLoop__process_events