-
Notifications
You must be signed in to change notification settings - Fork 6
/
select_object.py
60 lines (52 loc) · 1.97 KB
/
select_object.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
## @package PyHoot.select_object
# Select API for async_io.
## @file select_object.py Implementation of @ref PyHoot.select_object
import select
from . import base, common_events
class Select(base.Base):
"""
Select API for async_io.
Return events in the same protocol of Poll protocol
Args:
self._rlist: All the sockets that will be registered for reading
self._wlist: All the sockets that will be registered for writing
self._xlist: All the sockets that will be registered for errors
"""
def __init__(self):
"""initialization"""
super(Select, self).__init__()
self._rlist = []
self._wlist = []
self._xlist = []
def register(self, fd_socket, eventmask):
"""Add sockets to the lists it needs to be added
Args:
fd_socket: list of [file_descriptor of socket, socket]
eventmask: The events we want to register it with (poll protocol)
"""
socket = fd_socket[1]
if eventmask & common_events.CommonEvents.POLLIN:
self._rlist.append(socket)
if eventmask & common_events.CommonEvents.POLLOUT:
self._wlist.append(socket)
if eventmask & common_events.CommonEvents.POLLERR:
self._xlist.append(socket)
def poll(self):
"""Turn the results of a select into the format of poll"""
rlist, wlist, xlist = select.select(
self._rlist, self._wlist, self._xlist
)
self.logger.debug(
"select to poll \n rlist %s\nwlist %s\n xlist %s",
rlist, wlist, xlist
)
polled = {}
for s in rlist + wlist + xlist:
fd = s.fileno()
if s in rlist:
polled[fd] = common_events.CommonEvents.POLLIN
if s in wlist:
polled[fd] = common_events.CommonEvents.POLLOUT
if s in xlist:
polled[fd] = common_events.CommonEvents.POLLERR
return polled.items()