-
-
Notifications
You must be signed in to change notification settings - Fork 393
Closed
Description
When I modify the tutorial echo-client so that the sender closes the stream after one iteration, I expected the connection to close and the receiver to stop:
# echo-client.py
import sys
import trio
# arbitrary, but:
# - must be in between 1024 and 65535
# - can't be in use by some other program on your computer
# - must match what we set in our echo server
PORT = 12345
# How much memory to spend (at most) on each call to recv. Pretty arbitrary,
# but shouldn't be too big or too small.
BUFSIZE = 16384
async def sender(client_stream):
print("sender: started!")
while True:
data = b"async can sometimes be confusing, but I believe in you!"
print("sender: sending {!r}".format(data))
await client_stream.send_all(data)
await trio.sleep(1)
break # stop after one run
await client_stream.aclose() # Close stream
async def receiver(client_stream):
print("receiver: started!")
while True:
data = await client_stream.receive_some(BUFSIZE)
print("receiver: got data {!r}".format(data))
if not data:
print("receiver: connection closed")
sys.exit()
async def parent():
print("parent: connecting to 127.0.0.1:{}".format(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...")
nursery.start_soon(receiver, client_stream)
trio.run(parent)The server prints a connection closed but the client’s receiver() does not detect the stream being closed and does not finish.
I also found no other way to explicitly cancel the receiver() task when the sender() task is done.
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels