Skip to content

Commit

Permalink
aiocoap-cli: Perform library shutdown
Browse files Browse the repository at this point in the history
Contributes-To: #279
  • Loading branch information
chrysn committed Aug 17, 2022
1 parent f203ff4 commit e076e74
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions aiocoap/cli/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ def present(message, options, file=sys.stdout):
if file.isatty() and payload[-1] != '\n':
file.write("\n")

async def single_request(args, context=None):
async def single_request(args, context):
parser = build_parser()
options = parser.parse_args(args)

Expand All @@ -203,9 +203,6 @@ async def single_request(args, context=None):
except ValueError:
raise parser.error("Unknown method")

if context is None:
context = await aiocoap.Context.create_client_context()

if options.credentials is not None:
apply_credentials(context, options.credentials, parser.error)

Expand Down Expand Up @@ -335,6 +332,15 @@ async def single_request(args, context=None):
if options.observe and not requester.observation.cancelled:
requester.observation.cancel()

async def single_request_with_context(args):
"""Wrapper around single_request until sync_main gets made fully async, and
async context managers are used to manage contexts."""
context = await aiocoap.Context.create_client_context()
try:
await single_request(args, context)
finally:
await context.shutdown()

interactive_expecting_keyboard_interrupt = None

async def interactive():
Expand All @@ -355,7 +361,7 @@ async def interactive():
if line in (["help"], ["?"]):
line = ["--help"]
if line in (["quit"], ["q"], ["exit"]):
return
break

current_task = asyncio.create_task(
single_request(line, context=context),
Expand All @@ -377,14 +383,16 @@ async def interactive():
except Exception as e:
print("Unhandled exception raised: %s"%(e,))

await context.shutdown()

def sync_main(args=None):
# interactive mode is a little messy, that's why this is not using aiocoap.util.cli yet
if args is None:
args = sys.argv[1:]

if '--interactive' not in args:
try:
asyncio.run(single_request(args))
asyncio.run(single_request_with_context(args))
except KeyboardInterrupt:
sys.exit(3)
else:
Expand Down

0 comments on commit e076e74

Please sign in to comment.