37 lines
1.0 KiB
Python
37 lines
1.0 KiB
Python
import sys
|
|
import trio
|
|
|
|
PORT = 8081
|
|
|
|
|
|
async def sender(client_stream):
|
|
print("sender: started!")
|
|
while True:
|
|
data = b"async can sometimes be confusing, but I believe in you!"
|
|
print(f"sender: sending {data!r}")
|
|
await client_stream.send_all(data)
|
|
await trio.sleep(1)
|
|
|
|
|
|
async def receiver(client_stream):
|
|
print("receiver: started!")
|
|
async for data in client_stream:
|
|
print(f"receiver got data {data!r}")
|
|
print("received: connection closed")
|
|
sys.exit()
|
|
|
|
|
|
async def parent():
|
|
print(f"parent: connecting to 127.0.0.1:{PORT}")
|
|
client_stream = await trio.open_tcp_stream("127.0.0.1", PORT)
|
|
async with client_stream:
|
|
async with trio.open_nursery() as nursery:
|
|
print("parent: spawning sender...")
|
|
nursery.start_soon(sender, client_stream)
|
|
|
|
print("parent: spawning receiver...")
|
|
# noinspection PyTypeChecker
|
|
nursery.start_soon(receiver, client_stream)
|
|
|
|
if __name__ == '__main__':
|
|
trio.run(parent) |