From d3b4cc089fbf0166b8840e36f354f6575db2e4a5 Mon Sep 17 00:00:00 2001 From: "Mark E. Haase" Date: Thu, 11 Oct 2018 11:27:26 -0400 Subject: [PATCH] Update example client to use cancel scope (#22) If the server closes the connection, the client will quit immediately rather than waiting for the next user command on stdin. --- examples/client.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/examples/client.py b/examples/client.py index 6030b95..5123ebb 100644 --- a/examples/client.py +++ b/examples/client.py @@ -17,7 +17,6 @@ logging.basicConfig(level=logging.DEBUG) -logger = logging.getLogger() here = pathlib.Path(__file__).parent @@ -54,9 +53,11 @@ async def main(args): ssl_context = None try: logging.debug('Connecting to WebSocket…') - async with open_websocket_url(args.url, ssl_context) as conn: - logging.debug('Connected!') - await handle_connection(conn) + async with trio.open_nursery() as nursery: + async with open_websocket_url(args.url, ssl_context, + cancel_scope=nursery.cancel_scope) as conn: + logging.debug('Connected!') + await handle_connection(conn) logging.debug('Connection closed') except OSError as ose: logging.error('Connection attempt failed: %s', ose) @@ -67,7 +68,6 @@ async def handle_connection(connection): ''' Handle the connection. ''' while True: try: - logger.debug('top of loop') await trio.sleep(0.1) # allow time for connection logging cmd = await trio.run_sync_in_worker_thread(input, 'cmd> ', cancellable=True)