def poll(self, timeout=0.0): """Modified version of poll() from asyncore module""" if self.sock_map is None: Log.warning("Socket map is not registered to Gateway Looper") readable_lst = [] writable_lst = [] error_lst = [] if self.sock_map is not None: for fd, obj in self.sock_map.items(): is_r = obj.readable() is_w = obj.writable() if is_r: readable_lst.append(fd) if is_w and not obj.accepting: writable_lst.append(fd) if is_r or is_w: error_lst.append(fd) # Add wakeup fd readable_lst.append(self.pipe_r) Log.debug("Will select() with timeout: " + str(timeout) + ", with map: " + str(self.sock_map)) try: readable_lst, writable_lst, error_lst = \ select.select(readable_lst, writable_lst, error_lst, timeout) except select.error as err: Log.debug("Trivial error: " + str(err)) if err.args[0] != errno.EINTR: raise else: return Log.debug("Selected [r]: " + str(readable_lst) + " [w]: " + str(writable_lst) + " [e]: " + str(error_lst)) if self.pipe_r in readable_lst: Log.debug("Read from pipe") os.read(self.pipe_r, 1024) readable_lst.remove(self.pipe_r) if self.sock_map is not None: for fd in readable_lst: obj = self.sock_map.get(fd) if obj is None: continue asyncore.read(obj) for fd in writable_lst: obj = self.sock_map.get(fd) if obj is None: continue asyncore.write(obj) for fd in error_lst: obj = self.sock_map.get(fd) if obj is None: continue # pylint: disable=W0212 asyncore._exception(obj)
def handle_poll_results(self, msg): if "error" in msg: print "I don't know how we handle errors: %s" % (msg["error"], ) return r, w, e = msg["fds"] map = self.map for fd in r: obj = map.get(fd) if obj is None: continue asyncore.read(obj) for fd in w: obj = map.get(fd) if obj is None: continue asyncore.write(obj) for fd in e: obj = map.get(fd) if obj is None: continue asyncore._exception(obj)
def handle_poll_results(self, msg): if "error" in msg: print "I don't know how we handle errors: %s" % (msg["error"],) return r, w, e = msg["fds"] map = self.map for fd in r: obj = map.get(fd) if obj is None: continue asyncore.read(obj) for fd in w: obj = map.get(fd) if obj is None: continue asyncore.write(obj) for fd in e: obj = map.get(fd) if obj is None: continue asyncore._exception(obj)
def poll(timeout=0.0, map=None, mdisp=None): if map is None: map = asyncore.socket_map if mdisp is None: mdisp = multi_dispatcher if map: if mdisp.timeout != -1: timeout = min(timeout, mdisp.timeout/1000.0) r = []; w = []; e = [] for fd, obj in map.items(): is_r = obj.readable() is_w = obj.writable() if is_r: r.append(fd) if is_w: w.append(fd) if is_r or is_w: e.append(fd) if [] == r == w == e: if timeout > 0: time.sleep(timeout) else: try: r, w, e = select.select(r, w, e, timeout) except select.error, err: if err[0] != EINTR: raise else: return if [] == r == w == e: mdisp.socket_action(SOCKET_TIMEOUT, 0) return for fd in r: obj = map.get(fd) if obj is None: continue asyncore.read(obj) for fd in w: obj = map.get(fd) if obj is None: continue asyncore.write(obj) for fd in e: obj = map.get(fd) if obj is None: continue asyncore._exception(obj)
def test_readwriteexc(self): tr1 = exitingdummy() self.assertRaises(asyncore.ExitNow, asyncore.read, tr1) self.assertRaises(asyncore.ExitNow, asyncore.write, tr1) self.assertRaises(asyncore.ExitNow, asyncore._exception, tr1) tr2 = crashingdummy() asyncore.read(tr2) self.assertEqual(tr2.error_handled, True) tr2 = crashingdummy() asyncore.write(tr2) self.assertEqual(tr2.error_handled, True) tr2 = crashingdummy() asyncore._exception(tr2) self.assertEqual(tr2.error_handled, True)
def poll(map, timeout=0.0): """Asyncore poll function.""" if map: r = [] w = [] e = [] for fd, obj in map.items(): is_r = obj.readable() is_w = obj.writable() if is_r: r.append(fd) if is_w: w.append(fd) if is_r or is_w: e.append(fd) if [] == r == w == e: time.sleep(timeout) else: try: if use_select_emulation: r, w, e = clewn_select(r, w, e, timeout) else: r, w, e = select.select(r, w, e, timeout) except select.error, err: if err[0] != errno.EINTR: raise else: return for fd in r: obj = map.get(fd) if obj is None: continue asyncore.read(obj) for fd in w: obj = map.get(fd) if obj is None: continue asyncore.write(obj) for fd in e: obj = map.get(fd) if obj is None: continue asyncore._exception(obj)
def run(self, timeout=0.0): """Run the asyncore poll function.""" if self.socket_map: r = [] w = [] e = [] for fd, obj in self.socket_map.items(): is_r = obj.readable() is_w = obj.writable() if is_r: r.append(fd) if is_w: w.append(fd) if is_r or is_w: e.append(fd) if [] == r == w == e: time.sleep(timeout) else: try: if use_select_emulation: r, w, e = clewn_select(r, w, e, timeout, self) else: r, w, e = select.select(r, w, e, timeout) except select.error, err: if err[0] != errno.EINTR: raise else: return for fd in r: obj = self.socket_map.get(fd) if obj is None: continue asyncore.read(obj) for fd in w: obj = self.socket_map.get(fd) if obj is None: continue asyncore.write(obj) for fd in e: obj = self.socket_map.get(fd) if obj is None: continue asyncore._exception(obj)
def test_readwriteexc(self): # Check exception handling behavior of read, write and _exception # check that ExitNow exceptions in the object handler method # bubbles all the way up through asyncore read/write/_exception calls tr1 = exitingdummy() self.assertRaises(asyncore.ExitNow, asyncore.read, tr1) self.assertRaises(asyncore.ExitNow, asyncore.write, tr1) self.assertRaises(asyncore.ExitNow, asyncore._exception, tr1) # check that an exception other than ExitNow in the object handler # method causes the handle_error method to get called tr2 = crashingdummy() asyncore.read(tr2) self.assertEqual(tr2.error_handled, True) tr2 = crashingdummy() asyncore.write(tr2) self.assertEqual(tr2.error_handled, True) tr2 = crashingdummy() asyncore._exception(tr2) self.assertEqual(tr2.error_handled, True)
def update_event(self, inp=-1): self.set_output_val(0, asyncore._exception(self.input(0)))
class GatewayLooper(EventLooper): """A GatewayLooper, inheriting EventLooper It is a class wrapping Python's asyncore module (and selector) to dispatch events. This class can be used as a looper for an ``asyncore.dispatcher`` class, instead of calling ``asyncore.loop()``. As it is a subclass of EventLooper, it will execute in a while loop until the ``exit_loop()`` is called. In order to use this class, users first need to specify a socket map that maps from a file descriptor to ``asyncore.dispatcher`` class, using ``prepare_map()`` method. The GatewayLooper will dispatch ready events that are in the specified map. """ def __init__(self, socket_map): """Initializes a GatewayLooper instance :param socket_map: socket map used for asyncore.dispatcher """ super(GatewayLooper, self).__init__() self.sock_map = socket_map # Pipe used for wake up select self.pipe_r, self.pipe_w = os.pipe() self.started = time.time() Log.debug("Gateway Looper started time: " + str(time.asctime())) def do_wait(self): next_timeout = self._get_next_timeout_interval() if next_timeout > 0: self.poll(timeout=next_timeout) else: self.poll(timeout=0.0) def wake_up(self): os.write(self.pipe_w, "\n") Log.debug("Wake up called") def on_exit(self): super(GatewayLooper, self).on_exit() os.close(self.pipe_r) os.close(self.pipe_w) # pylint: disable=too-many-branches def poll(self, timeout=0.0): """Modified version of poll() from asyncore module""" if self.sock_map is None: Log.warning("Socket map is not registered to Gateway Looper") readable_lst = [] writable_lst = [] error_lst = [] if self.sock_map is not None: for fd, obj in self.sock_map.items(): is_r = obj.readable() is_w = obj.writable() if is_r: readable_lst.append(fd) if is_w and not obj.accepting: writable_lst.append(fd) if is_r or is_w: error_lst.append(fd) # Add wakeup fd readable_lst.append(self.pipe_r) Log.debug("Will select() with timeout: " + str(timeout) + ", with map: " + str(self.sock_map)) try: readable_lst, writable_lst, error_lst = \ select.select(readable_lst, writable_lst, error_lst, timeout) except select.error, err: Log.debug("Trivial error: " + err.message) if err.args[0] != errno.EINTR: raise else: return Log.debug("Selected [r]: " + str(readable_lst) + " [w]: " + str(writable_lst) + " [e]: " + str(error_lst)) if self.pipe_r in readable_lst: Log.debug("Read from pipe") os.read(self.pipe_r, 1024) readable_lst.remove(self.pipe_r) if self.sock_map is not None: for fd in readable_lst: obj = self.sock_map.get(fd) if obj is None: continue asyncore.read(obj) for fd in writable_lst: obj = self.sock_map.get(fd) if obj is None: continue asyncore.write(obj) for fd in error_lst: obj = self.sock_map.get(fd) if obj is None: continue # pylint: disable=W0212 asyncore._exception(obj)