def test_channel_from_connection(kernel): import multiprocessing p1, p2 = multiprocessing.Pipe() ch1 = Channel.from_Connection(p1) ch2 = Channel.from_Connection(p2) results = [] async def server(ch): async with ch: await ch.send("server hello world") results.append(await ch.recv()) async def client(ch): async with ch: msg = await ch.recv() results.append(msg) await ch.send("client hello world") async def main(ch1, ch2): await spawn(server(ch1)) await spawn(client(ch2)) kernel.run(main(ch1, ch2)) assert results == ["server hello world", "client hello world"]
def test_channel_from_connection(kernel): import multiprocessing p1, p2 = multiprocessing.Pipe() ch1 = Channel.from_Connection(p1) ch2 = Channel.from_Connection(p2) results = [] async def server(ch): async with ch: await ch.send('server hello world') results.append(await ch.recv()) async def client(ch): async with ch: msg = await ch.recv() results.append(msg) await ch.send('client hello world') async def main(ch1, ch2): await spawn(server(ch1)) await spawn(client(ch2)) kernel.run(main(ch1, ch2)) assert results == ['server hello world', 'client hello world']
def chs(): sock1, sock2 = socketpair() sock1_s = SocketStream(sock1) sock2_s = SocketStream(sock2) ch1 = Channel(sock1_s, sock1_s) ch2 = Channel(sock2_s, sock2_s) return (ch1, ch2) sock1, sock2 = socketpair() fileno1 = sock1.detach() ch1 = Channel(Stream(open(fileno1, 'rb', buffering=0)), Stream(open(fileno1, 'wb', buffering=0, closefd=False))) fileno2 = sock2.detach() ch2 = Channel(Stream(open(fileno2, 'rb', buffering=0)), Stream(open(fileno2, 'wb', buffering=0, closefd=False))) return (ch1, ch2)
async def listen_for_new_conns(task_group): while True: ch = Channel(chan) try: connection = await ch.accept(authkey=encoded) mlog.info(f'new connection created {connection}') await task_group.spawn(manage_single_connection, connection, currently_running_urls) await ch.close( ) # sort of strange that we need this? can we connect again later !? except ConnectionResetError as e: mlog.warning( 'client connection attempt did not terminate property')
async def auth(): ch = Channel(chan) async def connect(_ch=ch, authkey=encoded): connection = await _ch.connect(authkey=encoded) clog.debug(f'got connection {connection}') return connection async with TaskGroup(name='client auth', wait=any) as auth_or_exit: clog.info('waiting for sync services to start') exit_task = await auth_or_exit.spawn(exit, auth_or_exit) conn_task = await auth_or_exit.spawn(connect) connection = conn_task.result clog.debug(str(connection)) return connection
def chs(): ch1 = Channel(('localhost', 0)) ch1.bind() ch2 = copy.deepcopy(ch1) return ch1, ch2