Skip to content

Instantly share code, notes, and snippets.

@jenia
Last active August 4, 2020 21:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jenia/706d3d03bcc05806e96423184b0fcd41 to your computer and use it in GitHub Desktop.
Save jenia/706d3d03bcc05806e96423184b0fcd41 to your computer and use it in GitHub Desktop.
# echo-client.py
import sys
import trio
import time
# 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
async def sender(client_stream, flag):
print("sender: started!")
end_time = time.time() + 10
while time.time() < end_time:
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(0)
flag = False
print("Left the while 10 seconds loops")
async def receiver(client_stream, flag):
print("receiver: started!")
while(flag):
data = await client_stream.receive_some()
print("receiver: got data {!r}".format(data))
print("receiver: connection closed")
sys.exit()
async def start_server(flag):
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, flag)
print("parent: spawning receiver...")
nursery.start_soon(receiver, client_stream, flag)
async def parent():
async with trio.open_nursery() as nursery:
flag0 = True
flag1 = True
nursery.start_soon(start_server, flag0)
nursery.start_soon(start_server, flag1)
trio.run(parent)
#secho-server.py
import trio
from itertools import count
# Port is 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 client
PORT = 12345
CONNECTION_COUNTER = count()
async def echo_server(server_stream):
# Assign each connection a unique number to make our debug prints easier
# to understand when there are multiple simultaneous connections.
ident = next(CONNECTION_COUNTER)
print("echo_server {}: started".format(ident))
try:
async for data in server_stream:
print("echo_server {}: received data {!r}".format(ident, data))
await server_stream.send_all(data)
print("echo_server {}: connection closed".format(ident))
# FIXME: add discussion of MultiErrors to the tutorial, and use
# MultiError.catch here. (Not important in this case, but important if the
# server code uses nurseries internally.)
except Exception as exc:
# Unhandled exceptions will propagate into our parent and take
# down the whole program. If the exception is KeyboardInterrupt,
# that's what we want, but otherwise maybe not...
print("echo_server {}: crashed: {!r}".format(ident, exc))
async def main():
await trio.serve_tcp(echo_server, PORT)
# We could also just write 'trio.run(trio.serve_tcp, echo_server, PORT)', but real
# programs almost always end up doing other stuff too and then we'd have to go
# back and factor it out into a separate function anyway. So it's simplest to
# just make it a standalone function from the beginning.
trio.run(main)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment