def __init__(self, local_address, local_port, cmd_handler = None): self._cmd_handler = cmd_handler self._stream_server = StreamServer((local_address, local_port), self._accept, spawn = self._spawn_hook) self._stop = False self._ev_stop_getter, self._ev_stop_setter = socketpair() self._acceptors = [] self._queries = {} self._socks = {}
def run_cmd(args, timeout=None): _init() args = list(args) for i, x in enumerate(args): if isinstance(x, unicode): args[i] = x.encode("utf-8") sp = socket.socketpair() pid = os.fork() if pid == 0: # client try: os.dup2(sp[1].fileno(), 1) os.dup2(sp[1].fileno(), 2) sp[0].close() sp[1].close() os.execvp(args[0], args) except: stderr = os.fdopen(2, "w", 0) os.write(2, "failed to exec child process: %r\nPATH=%r" % (args, os.environ.get('PATH'))) traceback.print_exc(file=stderr) finally: os._exit(97) pid2status[pid] = event.AsyncResult() if not _nochild: def cb(): pid2status[pid].set(child_watcher.rstatus) child_watcher.stop() child_watcher = get_hub().loop.child(pid) child_watcher.start(cb) sp[1].close() chunks = [] # prevent loopexit. see test_run_cmd_trigger_loopexit in test_proc.py if timeout is None: timeout = 2 ** 30 timeout = Timeout(timeout) timeout.start() try: while 1: chunk = sp[0].recv(4096) if not chunk: break chunks.append(chunk) st = pid2status[pid].get() del pid2status[pid] return st, "".join(chunks) except Timeout, t: if t is not timeout: raise
def test_rawconn_error(): a, b = socketpair() def server_handler(stream): stream.close_with_error(NoRouteError()) with RawConn(a, server_handler, True) as server: with RawConn(b) as client: with client.new_stream() as c: with pytest.raises(NoRouteError): c.peek()
def test_rawconn(): a, b = socketpair() def server_handler(stream): data = stream.recv(16) stream.write(data) with RawConn(a, server_handler, True) as server: with RawConn(b) as client: with client.new_stream() as c: c.write('foobar') data = c.recv(16) assert data == 'foobar'
def _test_wait_read_invalid_switch(self, sleep): sock1, sock2 = socket.socketpair() try: p = gevent.spawn(util.wrap_errors(AssertionError, socket.wait_read), sock1.fileno()) gevent.get_hub().loop.run_callback(switch_None, p) if sleep is not None: gevent.sleep(sleep) result = p.get() assert isinstance(result, AssertionError), result assert 'Invalid switch' in str(result), repr(str(result)) finally: sock1.close() sock2.close()
def test_select_many_sockets(self): try: AF_UNIX = socket.AF_UNIX except AttributeError: AF_UNIX = None pairs = [socket.socketpair() for _ in range(10)] try: server_sel = selectors.GeventSelector() client_sel = selectors.GeventSelector() for i, pair in enumerate(pairs): server, client = pair server_sel.register(server, selectors.EVENT_READ, self.read_from_ready_socket_and_reply) client_sel.register(client, selectors.EVENT_READ, i) # Prime them all to be ready at once. data = str(i).encode('ascii') client.send(data) # Read and reply to all the clients.. # Everyone should be ready, so we ask not to block. # The call to gevent.idle() is there to make sure that # all event loop implementations (looking at you, libuv) # get a chance to poll for IO. Without it, libuv # doesn't find any results here. # Not blocking only works for AF_UNIX sockets, though. # If we got AF_INET (Windows) the data may need some time to # traverse through the layers. gevent.idle() self.run_selector_once( server_sel, timeout=-1 if pairs[0][0].family == AF_UNIX else 3) found = 0 for key, _ in client_sel.select(timeout=3): expected = str(key.data).encode('ascii') data = key.fileobj.recv(50) self.assertEqual(data, expected) found += 1 self.assertEqual(found, len(pairs)) finally: server_sel.close() client_sel.close() for pair in pairs: for s in pair: s.close()
def _check_selector(self, sel): server, client = socket.socketpair() try: sel.register(server, selectors.EVENT_READ, self.read_from_ready_socket_and_reply) glet = gevent.spawn(self.run_selector_once, sel) DATA = b'abcdef' client.send(DATA) data = client.recv( 50) # here is probably where we yield to the event loop self.assertEqual(data, DATA) finally: sel.close() server.close() client.close() glet.join(10) self.assertTrue(glet.ready())
def _socketpair(tmodel): if tmodel == ThreadingModel.THREADS: return tsocket.socketpair() elif tmodel == ThreadingModel.GEVENT: return gsocket.socketpair()
for sb in self.subscribers: if sb.deliverer: sb.deliverer.kill(block=False) _ws_refs = weakref.WeakValueDictionary() # To generate new ids for websocket on worker (warps after trillons of years): _ws_id_iterator = None _worker_count = 1 _rpc_map = {} _rpc_next_id = 0 (_call_endpoint, _handle_endpoint) = socket.socketpair(socket.AF_UNIX, socket.SOCK_SEQPACKET) def _ws_serialize(websocket): try: uid = websocket.environ['unique_id'] except KeyError: uid = _ws_id_iterator.next() websocket.environ['unique_id'] = uid return (websocket.handler.socket.fileno(), uid) def _ws_from_fd(fd): class PassiveWebSocket(WebSocket): __slots__ = ('__weakref__', 'unique_id')
def bench_gevent_forked_socketpair(loops): send, recv = gsocket.socketpair() return _do_sendall(loops, send, recv)
def bench_gevent_greenlet_default_socketpair(loops): send, recv = gsocket.socketpair() gevent.spawn(recvall, recv, None) return _sendall(loops, send, BIG_DATA)
def force_stop(self): for sb in self.subscribers: if sb.deliverer: sb.deliverer.kill(block=False) _ws_refs = weakref.WeakValueDictionary() # To generate new ids for websocket on worker (warps after trillons of years): _ws_id_iterator = None _worker_count = 1 _rpc_map = {} _rpc_next_id = 0 (_call_endpoint, _handle_endpoint) = socket.socketpair(socket.AF_UNIX, socket.SOCK_SEQPACKET) def _ws_serialize(websocket): try: uid = websocket.environ['unique_id'] except KeyError: uid = _ws_id_iterator.next() websocket.environ['unique_id'] = uid return (websocket.handler.socket.fileno(), uid) def _ws_from_fd(fd): class PassiveWebSocket(WebSocket): __slots__ = ('__weakref__', 'unique_id') def __init__(self, *args, **kwargs): WebSocket.__init__(self, *args, **kwargs)