async def coro2(): print("C2: Start") await switch() print("C2: a") await switch() print("C2: b") scheduler.add_coro(coro3()) await switch() print("C2: c") await sleep(1) print("C2: Stop")
async def server(): global count print("C1: Start server") # Setup a server socket. sock = socket.socket() sock.bind(SERVER_ADDR) sock.listen(BACKLOG) sock.setblocking(False) while True: await io(sock, selectors.EVENT_READ) conn, addr = sock.accept() print("Got connection: {} ({})".format(addr, count)) scheduler.add_coro(echo(conn)) count += 1
NUM_TESTERS = 50 SERVER_ADDR = ('localhost', 1234) FMT = b"TEST%05d" LEN = 9 async def echoclient(cid): sock = socket.socket() sock.setblocking(False) try: sock.connect(SERVER_ADDR) except BlockingIOError as e: pass for _ in range(10): await io(sock, selectors.EVENT_WRITE) testdata = FMT % cid sock.send(testdata) await io(sock, selectors.EVENT_READ) data = sock.recv(LEN) assert testdata == data, "Bad data: {} != {}".format(testdata, data) await sleep(0.2) sock.close() for cid in range(NUM_TESTERS): scheduler.add_coro(echoclient(cid)) scheduler.run()
async def coro2(): print("C2: Start") await switch() print("C2: a") await switch() print("C2: b") scheduler.add_coro(coro3()) await switch() print("C2: c") await sleep(1) print("C2: Stop") async def coro3(): print("C3: Start") await sleep(1) print("C3: a") await switch() print("C3: b") await switch() print("C3: c") await sleep(1) print("C3: Stop") scheduler.add_coro(coro1()) scheduler.add_coro(coro2()) scheduler.run()
SERVER_ADDR = ('localhost', 1234) FMT = b"TEST%05d" LEN = 9 async def echoclient(cid): sock = socket.socket() sock.setblocking(False) try: sock.connect(SERVER_ADDR) except BlockingIOError as e: pass for _ in range(10): await io(sock, selectors.EVENT_WRITE) testdata = FMT % cid sock.send(testdata) await io(sock, selectors.EVENT_READ) data = sock.recv(LEN) assert testdata == data, "Bad data: {} != {}".format(testdata, data) await sleep(0.2) sock.close() for cid in range(NUM_TESTERS): scheduler.add_coro(echoclient(cid)) scheduler.run()
sock = socket.socket() sock.bind(SERVER_ADDR) sock.listen(BACKLOG) sock.setblocking(False) while True: await io(sock, selectors.EVENT_READ) conn, addr = sock.accept() print("Got connection: {} ({})".format(addr, count)) scheduler.add_coro(echo(conn)) count += 1 async def echo(conn): global count conn.setblocking(False) while True: # Note: this is not a good echo implementation! await io(conn, selectors.EVENT_READ) data = conn.recv(100) if not data: break await io(conn, selectors.EVENT_WRITE) conn.send(data) conn.close() count -= 1 scheduler.add_coro(server()) scheduler.run()
# Setup a server socket. sock = socket.socket() sock.bind(SERVER_ADDR) sock.listen(BACKLOG) sock.setblocking(False) while True: await io(sock, selectors.EVENT_READ) conn, addr = sock.accept() print("Got connection: {} ({})".format(addr, count)) scheduler.add_coro(echo(conn)) count += 1 async def echo(conn): global count conn.setblocking(False) while True: # Note: this is not a good echo implementation! await io(conn, selectors.EVENT_READ) data = conn.recv(100) if not data: break await io(conn, selectors.EVENT_WRITE) conn.send(data) conn.close() count -= 1 scheduler.add_coro(server()) scheduler.run()