def test_connection_from_connection(kernel): import multiprocessing p1, p2 = multiprocessing.Pipe() c1 = Connection.from_Connection(p1) c2 = Connection.from_Connection(p2) results = [] async def server(c): async with c: await c.send('server hello world') results.append(await c.recv()) async def client(c): async with c: msg = await c.recv() results.append(msg) await c.send('client hello world') async def main(c1, c2): await spawn(server, c1) await spawn(client, c2) kernel.run(main(c1, c2)) assert results == ['server hello world', 'client hello world']
def conns(): sock1, sock2 = socketpair() sock1_s = SocketStream(sock1) sock2_s = SocketStream(sock2) c1 = Connection(sock1_s, sock1_s) c2 = Connection(sock2_s, sock2_s) return (c1, c2)
def test_connection_from_connection(kernel): import multiprocessing p1, p2 = multiprocessing.Pipe() c1 = Connection.from_Connection(p1) c2 = Connection.from_Connection(p2) results = [] async def server(c): async with c: await c.send('server hello world') results.append(await c.recv()) async def client(c): async with c: msg = await c.recv() results.append(msg) await c.send('client hello world') async def main(c1, c2): async with TaskGroup() as g: await g.spawn(server, c1) await g.spawn(client, c2) kernel.run(main(c1, c2)) assert results == ['server hello world', 'client hello world']
async def connect(self, *, authkey=None, attempts=None): nattempts = 0 while True: try: sock = socket.socket(self.family, socket.SOCK_STREAM) await sock.connect(self.address) sock_stream = sock.as_stream() c = Connection(sock_stream, sock_stream) #raise BaseException try: async with timeout_after(1): if authkey: await c.authenticate_client(authkey) return c except TaskTimeout: log.warning('Channel connection to %s timed out', self.address) await c.close() del c del sock_stream except OSError as e: if attempts is not None: if nattempts >= attempts: raise e else: nattempts += 1 else: log.error('Channel connection to %s failed', self.address, exc_info=True) await sock.close() await sleep(1)
def _launch(self): client_ch, server_ch = multiprocessing.Pipe() self.process = multiprocessing.Process(target=self.run_server, args=(server_ch, self.task_module_path)) self.process.start() server_ch.close() self.client_ch = Connection.from_Connection(client_ch) self.ident = self.process.ident return