dbhub: do not use as context manager, turn close exceptions into warnings, do not close devices early in worker

This commit is contained in:
Sebastien Bourdeauducq 2015-04-05 17:49:41 +08:00
parent 0ec7e9a98c
commit 9b46bc623a
3 changed files with 19 additions and 14 deletions

View File

@ -154,12 +154,15 @@ def run(with_file=False):
pdb = FlatFileDB(args.pdb) pdb = FlatFileDB(args.pdb)
pdb.hooks.append(SimpleParamLogger()) pdb.hooks.append(SimpleParamLogger())
rdb = ResultDB(lambda description: None, lambda mod: None) rdb = ResultDB(lambda description: None, lambda mod: None)
dbh = DBHub(ddb, pdb, rdb)
with DBHub(ddb, pdb, rdb) as dbh: try:
exp_inst = _build_experiment(dbh, args) exp_inst = _build_experiment(dbh, args)
rdb.build() rdb.build()
exp_inst.run() exp_inst.run()
exp_inst.analyze() exp_inst.analyze()
finally:
dbh.close_devices()
if args.hdf5 is not None: if args.hdf5 is not None:
with h5py.File(args.hdf5, "w") as f: with h5py.File(args.hdf5, "w") as f:

View File

@ -1,11 +1,15 @@
from collections import OrderedDict from collections import OrderedDict
import importlib import importlib
import logging
from artiq.protocols.sync_struct import Notifier from artiq.protocols.sync_struct import Notifier
from artiq.protocols.pc_rpc import Client, BestEffortClient from artiq.protocols.pc_rpc import Client, BestEffortClient
from artiq.master.results import result_dict_to_hdf5 from artiq.master.results import result_dict_to_hdf5
logger = logging.get_logger(__name__)
class ResultDB: class ResultDB:
def __init__(self, init_rt_results, update_rt_results): def __init__(self, init_rt_results, update_rt_results):
self.init_rt_results = init_rt_results self.init_rt_results = init_rt_results
@ -108,15 +112,11 @@ class DBHub:
"""Closes all active devices, in the opposite order as they were """Closes all active devices, in the opposite order as they were
requested.""" requested."""
for dev in reversed(list(self.active_devices.values())): for dev in reversed(list(self.active_devices.values())):
if isinstance(dev, (Client, BestEffortClient)): try:
dev.close_rpc() if isinstance(dev, (Client, BestEffortClient)):
elif hasattr(dev, "close"): dev.close_rpc()
dev.close() elif hasattr(dev, "close"):
dev.close()
except Exception as e:
logger.warning("Exception %r when closing device %r", e, dev)
self.active_devices.clear() self.active_devices.clear()
def __enter__(self):
return self
def __exit__(self, exc_type, exc_val, exc_tb):
self.close_devices()
return False # do not suppress exceptions within context

View File

@ -97,8 +97,9 @@ def main():
exp_inst = None exp_inst = None
rdb = ResultDB(init_rt_results, update_rt_results) rdb = ResultDB(init_rt_results, update_rt_results)
dbh = DBHub(ParentDDB, ParentPDB, rdb)
with DBHub(ParentDDB, ParentPDB, rdb) as dbh: try:
while True: while True:
obj = get_object() obj = get_object()
action = obj["action"] action = obj["action"]
@ -115,7 +116,6 @@ def main():
put_object({"action": "completed"}) put_object({"action": "completed"})
elif action == "run": elif action == "run":
exp_inst.run() exp_inst.run()
dbh.close_devices()
put_object({"action": "completed"}) put_object({"action": "completed"})
elif action == "analyze": elif action == "analyze":
exp_inst.analyze() exp_inst.analyze()
@ -129,6 +129,8 @@ def main():
put_object({"action": "completed"}) put_object({"action": "completed"})
elif action == "terminate": elif action == "terminate":
break break
finally:
dbh.close_devices()
if __name__ == "__main__": if __name__ == "__main__":
main() main()