Last active
August 1, 2020 16:27
-
-
Save jenia/3547bf0760a18ca430a09817af16f6b6 to your computer and use it in GitHub Desktop.
How to send requests for 60 seconds and wait for all responses
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#echo-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) | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 | |
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(0) | |
async def receiver(client_stream): | |
print("receiver: started!") | |
async for data in client_stream: | |
print("receiver: got data {!r}".format(data)) | |
print("receiver: connection closed") | |
sys.exit() | |
async def start_server(): | |
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) | |
async def parent(): | |
async with trio.open_nursery() as nursery: | |
nursery.start_soon(start_server) | |
nursery.start_soon(start_server) | |
trio.run(parent) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment