frontend: use sipyco SignalHandler (#1063)

This commit is contained in:
Sebastien Bourdeauducq 2022-05-27 15:17:27 +08:00
parent df1b19082c
commit 7cff63e539
4 changed files with 64 additions and 35 deletions

View File

@ -9,6 +9,7 @@ import re
from sipyco.pc_rpc import Server
from sipyco import common_args
from sipyco.logging_tools import log_with_name
from sipyco.asyncio_tools import SignalHandler
from artiq.coredevice.comm_mgmt import Request, Reply
@ -76,6 +77,9 @@ def main():
common_args.init_logger_from_args(args)
loop = asyncio.get_event_loop()
try:
signal_handler = SignalHandler()
signal_handler.setup()
try:
get_logs_task = asyncio.ensure_future(
get_logs_sim(args.core_addr) if args.simulation else get_logs(args.core_addr))
@ -83,7 +87,11 @@ def main():
server = Server({"corelog": PingTarget()}, None, True)
loop.run_until_complete(server.start(common_args.bind_address_from_args(args), args.port))
try:
loop.run_until_complete(server.wait_terminate())
_, pending = loop.run_until_complete(asyncio.wait(
[signal_handler.wait_terminate(), server.wait_terminate()],
return_when=asyncio.FIRST_COMPLETED))
for task in pending:
task.cancel()
finally:
loop.run_until_complete(server.stop())
finally:
@ -92,6 +100,8 @@ def main():
loop.run_until_complete(get_logs_task)
except asyncio.CancelledError:
pass
finally:
signal_handler.teardown()
finally:
loop.close()

View File

@ -6,7 +6,7 @@ import asyncio
import struct
from enum import Enum
from sipyco.asyncio_tools import AsyncioServer
from sipyco.asyncio_tools import AsyncioServer, SignalHandler
from sipyco.pc_rpc import Server
from sipyco import common_args
@ -197,6 +197,9 @@ def main():
bind_address = common_args.bind_address_from_args(args)
loop = asyncio.get_event_loop()
try:
signal_handler = SignalHandler()
signal_handler.setup()
try:
monitor_mux = MonitorMux()
comm_moninj = CommMonInj(monitor_mux.monitor_cb, monitor_mux.injection_status_cb)
@ -209,13 +212,19 @@ def main():
server = Server({"moninj_proxy": PingTarget()}, None, True)
loop.run_until_complete(server.start(bind_address, args.port_control))
try:
loop.run_until_complete(server.wait_terminate())
_, pending = loop.run_until_complete(asyncio.wait(
[signal_handler.wait_terminate(), server.wait_terminate()],
return_when=asyncio.FIRST_COMPLETED))
for task in pending:
task.cancel()
finally:
loop.run_until_complete(server.stop())
finally:
loop.run_until_complete(proxy_server.stop())
finally:
loop.run_until_complete(comm_moninj.close())
finally:
signal_handler.teardown()
finally:
loop.close()

View File

@ -10,7 +10,7 @@ from sipyco.sync_struct import Publisher
from sipyco.logging_tools import Server as LoggingServer
from sipyco.broadcast import Broadcaster
from sipyco import common_args
from sipyco.asyncio_tools import atexit_register_coroutine
from sipyco.asyncio_tools import atexit_register_coroutine, SignalHandler
from artiq import __version__ as artiq_version
from artiq.master.log import log_args, init_log
@ -76,6 +76,9 @@ def main():
log_forwarder = init_log(args)
loop = asyncio.get_event_loop()
atexit.register(loop.close)
signal_handler = SignalHandler()
signal_handler.setup()
atexit.register(signal_handler.teardown)
bind = common_args.bind_address_from_args(args)
server_broadcast = Broadcaster()
@ -155,7 +158,7 @@ def main():
atexit_register_coroutine(server_logging.stop)
print("ARTIQ master is now ready.")
loop.run_forever()
loop.run_until_complete(signal_handler.wait_terminate())
if __name__ == "__main__":
main()

View File

@ -3,6 +3,8 @@
import argparse
import asyncio
from sipyco.asyncio_tools import SignalHandler
from artiq.coredevice.comm_moninj import *
@ -20,6 +22,9 @@ def main():
args = get_argparser().parse_args()
loop = asyncio.get_event_loop()
try:
signal_handler = SignalHandler()
signal_handler.setup()
try:
comm = CommMonInj(
lambda channel, probe, value: print("0x{:06x}: {}".format(channel, value)),
@ -28,9 +33,11 @@ def main():
try:
for channel in args.channel:
comm.monitor_probe(True, channel, 0)
loop.run_forever()
loop.run_until_complete(signal_handler.wait_terminate())
finally:
loop.run_until_complete(comm.close())
finally:
signal_handler.teardown()
finally:
loop.close()