Debian Codegraph @

lib/ClusterShell/Engine/Select.py

raw symbols SCIP
1 #
2 # Copyright (C) 2009-2016 CEA/DAM
3 # Copyright (C) 2009-2012 Henri Doreau <[email protected]>
4 # Copyright (C) 2009-2012 Aurelien Degremont <[email protected]>
5 # Copyright (C) 2016 Stephane Thiell <[email protected]>
6 #
7 # This file is part of ClusterShell.
8 #
9 # ClusterShell is free software; you can redistribute it and/or
10 # modify it under the terms of the GNU Lesser General Public
11 # License as published by the Free Software Foundation; either
12 # version 2.1 of the License, or (at your option) any later version.
13 #
14 # ClusterShell is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 # Lesser General Public License for more details.
18 #
19 # You should have received a copy of the GNU Lesser General Public
20 # License along with ClusterShell; if not, write to the Free Software
21 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22
23 """
24 A select() based ClusterShell Engine.
25
26 The select() system call is available on almost every UNIX-like systems.
27 """
28
29 import errno
30 import logging
31 import select
32 import sys
33 import time
34
35 from ClusterShell.Engine.Engine import Engine, E_READ, E_WRITE
36 from ClusterShell.Engine.Engine import EngineTimeoutException
37 from ClusterShell.Worker.EngineClient import EngineClientEOF
38
39
40 class EngineSelect(Engine):
41 """
42 Select Engine
43
44 ClusterShell engine using the select.select mechanism
45 """
46
47 identifier = "select"
48
49 def __init__(self, info):
50 """
51 Initialize Engine.
52 """
53 Engine.__init__(self, info)
54 self._fds_r = []
55 self._fds_w = []
56
57 def _register_specific(self, fd, event):
58 """
59 Engine-specific fd registering. Called by Engine register.
60 """
61 if event & E_READ:
62 self._fds_r.append(fd)
63 else:
64 assert event & E_WRITE
65 self._fds_w.append(fd)
66
67 def _unregister_specific(self, fd, ev_is_set):
68 """
69 Engine-specific fd unregistering. Called by Engine unregister.
70 """
71 if ev_is_set or True:
72 if fd in self._fds_r:
73 self._fds_r.remove(fd)
74 if fd in self._fds_w:
75 self._fds_w.remove(fd)
76
77 def _modify_specific(self, fd, event, setvalue):
78 """
79 Engine-specific modifications after a interesting event change
80 for a file descriptor. Called automatically by Engine
81 register/unregister and set_events(). For the select() engine,
82 it appends/remove the fd to/from the concerned fd_sets.
83 """
84 self._debug("MODSPEC fd=%d event=%x setvalue=%d" % (fd, event,
85 setvalue))
86 if setvalue:
87 self._register_specific(fd, event)
88 else:
89 self._unregister_specific(fd, True)
90
91 def runloop(self, timeout):
92 """
93 Select engine run(): start clients and properly get replies
94 """
95 if not timeout:
96 timeout = -1
97
98 start_time = time.time()
99
100 # run main event loop...
101 while self.evlooprefcnt > 0:
102 self._debug("LOOP evlooprefcnt=%d (reg_clifds=%s) (timers=%d)" %
103 (self.evlooprefcnt, self.reg_clifds.keys(), len(self.timerq)))
104 try:
105 timeo = self.timerq.nextfire_delay()
106 if timeout > 0 and timeo >= timeout:
107 # task timeout may invalidate clients timeout
108 self.timerq.clear()
109 timeo = timeout
110 elif timeo == -1:
111 timeo = timeout
112
113 self._current_loopcnt += 1
114 if timeo >= 0:
115 r_ready, w_ready, x_ready = \
116 select.select(self._fds_r, self._fds_w, [], timeo)
117 else:
118 # no timeout specified, do not supply the timeout argument
119 r_ready, w_ready, x_ready = \
120 select.select(self._fds_r, self._fds_w, [])
121 except select.error as ex:
122 # might get interrupted by a signal
123 if ex.args[0] == errno.EINTR:
124 continue
125 elif ex.args[0] in (errno.EINVAL, errno.EBADF, errno.ENOMEM):
126 msg = "Increase RLIMIT_NOFILE?"
127 logging.getLogger(__name__).error(msg)
128 raise
129
130 # iterate over fd on which events occurred
131 for fd in set(r_ready) | set(w_ready):
132
133 # get client instance
134 client, stream = self._fd2client(fd)
135 if client is None:
136 continue
137
138 fdev = stream.evmask
139 sname = stream.name
140
141 # process this stream
142 self._current_stream = stream
143
144 # check for possible unblocking read on this fd
145 if fd in r_ready:
146 self._debug("R_READY fd=%d %s (%s)" % (fd,
147 client.__class__.__name__, client.streams))
148 assert fdev & E_READ
149 assert stream.events & fdev
150 self.modify(client, sname, 0, fdev)
151 try:
152 client._handle_read(sname)
153 except EngineClientEOF:
154 self._debug("EngineClientEOF %s" % client)
155 self.remove_stream(client, stream)
156
157 # check for writing
158 if fd in w_ready:
159 self._debug("W_READY fd=%d %s (%s)" % (fd,
160 client.__class__.__name__, client.streams))
161 assert fdev == E_WRITE
162 assert stream.events & fdev
163 self.modify(client, sname, 0, fdev)
164 client._handle_write(sname)
165
166 # post processing
167 self._current_stream = None
168
169 # apply any changes occurred during processing
170 if client.registered:
171 self.set_events(client, stream)
172
173 # check for task runloop timeout
174 if timeout > 0 and time.time() >= start_time + timeout:
175 raise EngineTimeoutException()
176
177 # process clients timeout
178 self.fire_timers()
179
180 self._debug("LOOP EXIT evlooprefcnt=%d (reg_clifds=%s) (timers=%d)" %
181 (self.evlooprefcnt, self.reg_clifds, len(self.timerq)))