frontend: use atexit_register_coroutine in other tools

This commit is contained in:
Sebastien Bourdeauducq 2015-11-11 16:22:12 +08:00
parent 69f5e378fe
commit 4166f4e928
5 changed files with 31 additions and 24 deletions

View File

@ -15,7 +15,7 @@ from artiq.protocols.pc_rpc import AsyncioClient, Server
from artiq.protocols.logging import (LogForwarder, from artiq.protocols.logging import (LogForwarder,
parse_log_message, log_with_name, parse_log_message, log_with_name,
SourceFilter) SourceFilter)
from artiq.tools import TaskObject, Condition from artiq.tools import TaskObject, Condition, atexit_register_coroutine
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@ -311,19 +311,19 @@ def main():
asyncio.set_event_loop(loop) asyncio.set_event_loop(loop)
else: else:
loop = asyncio.get_event_loop() loop = asyncio.get_event_loop()
atexit.register(lambda: loop.close()) atexit.register(loop.close)
logfwd = LogForwarder(args.server, args.port_logging, logfwd = LogForwarder(args.server, args.port_logging,
args.retry_master) args.retry_master)
logfwd.addFilter(source_adder) logfwd.addFilter(source_adder)
root_logger.addHandler(logfwd) root_logger.addHandler(logfwd)
logfwd.start() logfwd.start()
atexit.register(lambda: loop.run_until_complete(logfwd.stop())) atexit_register_coroutine(logfwd.stop)
ctlmgr = ControllerManager(args.server, args.port_notify, ctlmgr = ControllerManager(args.server, args.port_notify,
args.retry_master) args.retry_master)
ctlmgr.start() ctlmgr.start()
atexit.register(lambda: loop.run_until_complete(ctlmgr.stop())) atexit_register_coroutine(ctlmgr.stop)
class CtlMgrRPC: class CtlMgrRPC:
retry_now = ctlmgr.retry_now retry_now = ctlmgr.retry_now
@ -331,7 +331,7 @@ def main():
rpc_target = CtlMgrRPC() rpc_target = CtlMgrRPC()
rpc_server = Server({"ctlmgr": rpc_target}, builtin_terminate=True) rpc_server = Server({"ctlmgr": rpc_target}, builtin_terminate=True)
loop.run_until_complete(rpc_server.start(args.bind, args.bind_port)) loop.run_until_complete(rpc_server.start(args.bind, args.bind_port))
atexit.register(lambda: loop.run_until_complete(rpc_server.stop())) atexit_register_coroutine(rpc_server.stop)
loop.run_until_complete(rpc_server.wait_terminate()) loop.run_until_complete(rpc_server.wait_terminate())

View File

@ -10,7 +10,7 @@ import os
from quamash import QEventLoop, QtGui, QtCore from quamash import QEventLoop, QtGui, QtCore
from pyqtgraph import dockarea from pyqtgraph import dockarea
from artiq.tools import verbosity_args, init_logger, artiq_dir from artiq.tools import *
from artiq.protocols.pc_rpc import AsyncioClient from artiq.protocols.pc_rpc import AsyncioClient
from artiq.gui.models import ModelSubscriber from artiq.gui.models import ModelSubscriber
from artiq.gui import state, explorer, moninj, datasets, schedule, log, console from artiq.gui import state, explorer, moninj, datasets, schedule, log, console
@ -52,12 +52,6 @@ class MainWindow(QtGui.QMainWindow):
self.restoreGeometry(QtCore.QByteArray(state)) self.restoreGeometry(QtCore.QByteArray(state))
def atexit_register_coroutine(coroutine, loop=None):
if loop is None:
loop = asyncio.get_event_loop()
atexit.register(lambda: loop.run_until_complete(coroutine()))
def main(): def main():
args = get_argparser().parse_args() args = get_argparser().parse_args()
init_logger(args) init_logger(args)

View File

@ -10,8 +10,7 @@ from functools import partial
import numpy as np import numpy as np
import aiohttp import aiohttp
from artiq.tools import verbosity_args, init_logger from artiq.tools import *
from artiq.tools import TaskObject
from artiq.protocols.sync_struct import Subscriber from artiq.protocols.sync_struct import Subscriber
from artiq.protocols.pc_rpc import Server from artiq.protocols.pc_rpc import Server
from artiq.protocols import pyon from artiq.protocols import pyon
@ -239,23 +238,23 @@ def main():
init_logger(args) init_logger(args)
loop = asyncio.get_event_loop() loop = asyncio.get_event_loop()
atexit.register(lambda: loop.close()) atexit.register(loop.close)
writer = DBWriter(args.baseurl_db, writer = DBWriter(args.baseurl_db,
args.user_db, args.password_db, args.user_db, args.password_db,
args.database, args.table) args.database, args.table)
writer.start() writer.start()
atexit.register(lambda: loop.run_until_complete(writer.stop())) atexit_register_coroutine(writer.stop)
filter = Filter(args.pattern_file) filter = Filter(args.pattern_file)
rpc_server = Server({"influxdb_filter": filter}, builtin_terminate=True) rpc_server = Server({"influxdb_filter": filter}, builtin_terminate=True)
loop.run_until_complete(rpc_server.start(args.bind, args.bind_port)) loop.run_until_complete(rpc_server.start(args.bind, args.bind_port))
atexit.register(lambda: loop.run_until_complete(rpc_server.stop())) atexit_register_coroutine(rpc_server.stop)
reader = MasterReader(args.server_master, args.port_master, reader = MasterReader(args.server_master, args.port_master,
args.retry_master, filter._filter, writer) args.retry_master, filter._filter, writer)
reader.start() reader.start()
atexit.register(lambda: loop.run_until_complete(reader.stop())) atexit_register_coroutine(reader.stop)
loop.run_until_complete(rpc_server.wait_terminate()) loop.run_until_complete(rpc_server.wait_terminate())

View File

@ -5,6 +5,7 @@ import argparse
import atexit import atexit
import os import os
from artiq.tools import atexit_register_coroutine
from artiq.protocols.pc_rpc import Server as RPCServer from artiq.protocols.pc_rpc import Server as RPCServer
from artiq.protocols.sync_struct import Publisher from artiq.protocols.sync_struct import Publisher
from artiq.protocols.logging import Server as LoggingServer from artiq.protocols.logging import Server as LoggingServer
@ -59,12 +60,12 @@ def main():
asyncio.set_event_loop(loop) asyncio.set_event_loop(loop)
else: else:
loop = asyncio.get_event_loop() loop = asyncio.get_event_loop()
atexit.register(lambda: loop.close()) atexit.register(loop.close)
device_db = DeviceDB(args.device_db) device_db = DeviceDB(args.device_db)
dataset_db = DatasetDB(args.dataset_db) dataset_db = DatasetDB(args.dataset_db)
dataset_db.start() dataset_db.start()
atexit.register(lambda: loop.run_until_complete(dataset_db.stop())) atexit_register_coroutine(dataset_db.stop)
if args.git: if args.git:
repo_backend = GitBackend(args.repository) repo_backend = GitBackend(args.repository)
@ -90,7 +91,7 @@ def main():
"scheduler_get_status": scheduler.get_status "scheduler_get_status": scheduler.get_status
}) })
scheduler.start() scheduler.start()
atexit.register(lambda: loop.run_until_complete(scheduler.stop())) atexit_register_coroutine(scheduler.stop)
server_control = RPCServer({ server_control = RPCServer({
"master_device_db": device_db, "master_device_db": device_db,
@ -100,7 +101,7 @@ def main():
}) })
loop.run_until_complete(server_control.start( loop.run_until_complete(server_control.start(
args.bind, args.port_control)) args.bind, args.port_control))
atexit.register(lambda: loop.run_until_complete(server_control.stop())) atexit_register_coroutine(server_control.stop)
server_notify = Publisher({ server_notify = Publisher({
"schedule": scheduler.notifier, "schedule": scheduler.notifier,
@ -111,12 +112,12 @@ def main():
}) })
loop.run_until_complete(server_notify.start( loop.run_until_complete(server_notify.start(
args.bind, args.port_notify)) args.bind, args.port_notify))
atexit.register(lambda: loop.run_until_complete(server_notify.stop())) atexit_register_coroutine(server_notify.stop)
server_logging = LoggingServer() server_logging = LoggingServer()
loop.run_until_complete(server_logging.start( loop.run_until_complete(server_logging.start(
args.bind, args.port_logging)) args.bind, args.port_logging))
atexit.register(lambda: loop.run_until_complete(server_logging.stop())) atexit_register_coroutine(server_logging.stop)
loop.run_forever() loop.run_forever()

View File

@ -7,6 +7,7 @@ import asyncio
import time import time
import collections import collections
import os import os
import atexit
import numpy as np import numpy as np
@ -14,6 +15,12 @@ from artiq.language.environment import is_experiment
from artiq.protocols import pyon from artiq.protocols import pyon
__all__ = ["artiq_dir", "parse_arguments", "elide", "short_format", "file_import",
"get_experiment", "verbosity_args", "simple_network_args", "init_logger",
"atexit_register_coroutine", "exc_to_warning", "asyncio_wait_or_cancel",
"TaskObject", "Condition", "workaround_asyncio263"]
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
artiq_dir = os.path.join(os.path.abspath(os.path.dirname(__file__))) artiq_dir = os.path.join(os.path.abspath(os.path.dirname(__file__)))
@ -120,6 +127,12 @@ def init_logger(args):
logging.basicConfig(level=logging.WARNING + args.quiet*10 - args.verbose*10) logging.basicConfig(level=logging.WARNING + args.quiet*10 - args.verbose*10)
def atexit_register_coroutine(coroutine, loop=None):
if loop is None:
loop = asyncio.get_event_loop()
atexit.register(lambda: loop.run_until_complete(coroutine()))
async def exc_to_warning(coro): async def exc_to_warning(coro):
try: try:
await coro await coro