Skip to content

Commit

Permalink
Update example client to use cancel scope (#22)
Browse files Browse the repository at this point in the history
If the server closes the connection, the client will quit immediately
rather than waiting for the next user command on stdin.
  • Loading branch information
mehaase committed Oct 11, 2018
1 parent c77ee8f commit d3b4cc0
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions examples/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@


logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger()
here = pathlib.Path(__file__).parent


Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down

0 comments on commit d3b4cc0

Please sign in to comment.