From d95a9cac9a233e0422c39f8c2f7b582f2c8193b9 Mon Sep 17 00:00:00 2001 From: Sebastien Bourdeauducq Date: Sun, 8 Mar 2015 17:27:27 +0100 Subject: [PATCH] move realtime result registration into dbh, simplify syntax --- artiq/frontend/artiq_run.py | 2 +- artiq/language/db.py | 25 ++++--------------- artiq/master/worker_db.py | 24 ++++++++++++++++-- artiq/master/worker_impl.py | 18 +------------ .../repository/flopping_f_simulation.py | 8 +++--- 5 files changed, 32 insertions(+), 45 deletions(-) diff --git a/artiq/frontend/artiq_run.py b/artiq/frontend/artiq_run.py index 10f2375c1..bb4d03bea 100755 --- a/artiq/frontend/artiq_run.py +++ b/artiq/frontend/artiq_run.py @@ -98,7 +98,7 @@ def main(): ddb = FlatFileDB(args.ddb) pdb = FlatFileDB(args.pdb) pdb.hooks.append(SimpleParamLogger()) - rdb = ResultDB(set()) + rdb = ResultDB(lambda description: None, lambda mod: None) dbh = DBHub(ddb, pdb, rdb) try: if args.elf: diff --git a/artiq/language/db.py b/artiq/language/db.py index be45baed2..117a23835 100644 --- a/artiq/language/db.py +++ b/artiq/language/db.py @@ -1,6 +1,5 @@ """ Connection to device, parameter and result database. - """ class _AttributeKind: @@ -8,16 +7,13 @@ class _AttributeKind: class Device(_AttributeKind): - """Represents a device for ``AutoDB`` to process. - - """ + """Represents a device for ``AutoDB`` to process.""" pass class NoDefault: """Represents the absence of a default value for ``Parameter`` and ``Argument``. - """ pass @@ -28,7 +24,6 @@ class Parameter(_AttributeKind): :param default: Default value of the parameter to be used if not found in the database. - """ def __init__(self, default=NoDefault): self.default = default @@ -40,16 +35,13 @@ class Argument(_AttributeKind): :param default: Default value of the argument to be used if not specified at instance creation. - """ def __init__(self, default=NoDefault): self.default = default class Result(_AttributeKind): - """Represents a result for ``AutoDB`` to process. - - """ + """Represents a result for ``AutoDB`` to process.""" pass @@ -63,14 +55,11 @@ class AutoDB: :param dbh: database hub to use. If ``None``, all devices and parameters must be supplied as keyword arguments, and reporting results and modifying parameters is not supported. - """ class DBKeys: pass - @staticmethod - def realtime_results(): - return dict() + realtime_results = dict() def __init__(self, dbh=None, **kwargs): self.dbh = dbh @@ -92,8 +81,9 @@ class AutoDB: except KeyError: raise KeyError("Device '{}' not found".format(k)) object.__setattr__(self, k, dev) - self.build() + if self.dbh is not None: + self.dbh.init_results(self.realtime_results) def __getattr__(self, name): ak = getattr(self.DBKeys, name) @@ -130,16 +120,11 @@ class AutoDB: else: raise ValueError - @classmethod - def get_realtime_results(): - return dict() - def build(self): """This is called by ``__init__`` after the parameter initialization is done. The user may overload this method to complete the object's initialization with all parameters available. - """ pass diff --git a/artiq/master/worker_db.py b/artiq/master/worker_db.py index 3b10463f1..98e40e62f 100644 --- a/artiq/master/worker_db.py +++ b/artiq/master/worker_db.py @@ -7,10 +7,29 @@ from artiq.master.results import result_dict_to_hdf5 class ResultDB: - def __init__(self, realtime_results): - self.realtime_data = Notifier({x: [] for x in realtime_results}) + def __init__(self, init_rt_results, update_rt_results): + self.init_rt_results = init_rt_results + self.update_rt_results = update_rt_results + + def init(self, rtr_description): + assert not hasattr(self, "realtime_data") + assert not hasattr(self, "data") + + realtime_results_set = set() + for rtr in rtr_description.keys(): + if isinstance(rtr, tuple): + for e in rtr: + realtime_results_set.add(e) + else: + realtime_results_set.add(rtr) + + self.realtime_data = Notifier({x: [] for x in realtime_results_set}) self.data = Notifier(dict()) + self.init_rt_results(rtr_description) + self.realtime_data.publish = lambda notifier, data: \ + self.update_rt_results(data) + def _request(self, name): try: return self.realtime_data[name] @@ -64,6 +83,7 @@ class DBHub: self.get_parameter = pdb.request self.set_parameter = pdb.set + self.init_results = rdb.init self.get_result = rdb.request self.set_result = rdb.set diff --git a/artiq/master/worker_impl.py b/artiq/master/worker_impl.py index 5f8ef4891..6f92a871e 100644 --- a/artiq/master/worker_impl.py +++ b/artiq/master/worker_impl.py @@ -52,10 +52,6 @@ init_rt_results = make_parent_action("init_rt_results", "description") update_rt_results = make_parent_action("update_rt_results", "mod") -def publish_rt_results(notifier, data): - update_rt_results(data) - - class Scheduler: run_queued = make_parent_action("scheduler_run_queued", "run_params") cancel_queued = make_parent_action("scheduler_cancel_queued", "rid") @@ -81,19 +77,7 @@ def run(rid, run_params): start_time = time.localtime() exp = get_exp(run_params["file"], run_params["experiment"]) - realtime_results = exp.realtime_results() - init_rt_results(realtime_results) - - realtime_results_set = set() - for rr in realtime_results.keys(): - if isinstance(rr, tuple): - for e in rr: - realtime_results_set.add(e) - else: - realtime_results_set.add(rr) - rdb = ResultDB(realtime_results_set) - rdb.realtime_data.publish = publish_rt_results - + rdb = ResultDB(init_rt_results, update_rt_results) dbh = DBHub(ParentDDB, ParentPDB, rdb) try: try: diff --git a/examples/master/repository/flopping_f_simulation.py b/examples/master/repository/flopping_f_simulation.py index c2624695b..0a509336d 100644 --- a/examples/master/repository/flopping_f_simulation.py +++ b/examples/master/repository/flopping_f_simulation.py @@ -40,11 +40,9 @@ class FloppingF(Experiment, AutoDB): flopping_freq = Parameter() - @staticmethod - def realtime_results(): - return { - ("frequency", "brightness"): "xy" - } + realtime_results = { + ("frequency", "brightness"): "xy" + } def run(self): for i in range(self.npoints):