artiq_client: handle Ctrl-C gracefully

This commit is contained in:
Sébastien Bourdeauducq 2024-02-27 15:46:23 +08:00
parent 002325be17
commit 450fe91e93
1 changed files with 13 additions and 5 deletions

View File

@ -22,6 +22,7 @@ from sipyco.pc_rpc import Client
from sipyco.sync_struct import Subscriber from sipyco.sync_struct import Subscriber
from sipyco.broadcast import Receiver from sipyco.broadcast import Receiver
from sipyco import common_args, pyon from sipyco import common_args, pyon
from sipyco.asyncio_tools import SignalHandler
from artiq.tools import (scale_from_metadata, short_format, parse_arguments, from artiq.tools import (scale_from_metadata, short_format, parse_arguments,
parse_devarg_override) parse_devarg_override)
@ -278,13 +279,20 @@ def _run_subscriber(host, port, subscriber):
loop = asyncio.new_event_loop() loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop) asyncio.set_event_loop(loop)
try: try:
loop.run_until_complete(subscriber.connect(host, port)) signal_handler = SignalHandler()
signal_handler.setup()
try: try:
loop.run_until_complete(asyncio.wait_for(subscriber.receive_task, loop.run_until_complete(subscriber.connect(host, port))
None)) try:
print("Connection to master lost") _, pending = loop.run_until_complete(asyncio.wait(
[signal_handler.wait_terminate(), subscriber.receive_task],
return_when=asyncio.FIRST_COMPLETED))
for task in pending:
task.cancel()
finally:
loop.run_until_complete(subscriber.close())
finally: finally:
loop.run_until_complete(subscriber.close()) signal_handler.teardown()
finally: finally:
loop.close() loop.close()