2014-10-05 16:25:31 +08:00
|
|
|
import sys
|
2015-02-22 04:42:26 +08:00
|
|
|
import time
|
2014-10-05 16:25:31 +08:00
|
|
|
|
2015-01-17 19:38:20 +08:00
|
|
|
from artiq.protocols import pyon
|
|
|
|
from artiq.tools import file_import
|
2015-02-22 05:28:18 +08:00
|
|
|
from artiq.master.worker_db import DBHub, ResultDB
|
|
|
|
from artiq.master.results import get_hdf5_output
|
2014-12-08 19:22:02 +08:00
|
|
|
|
|
|
|
|
2014-12-31 17:41:22 +08:00
|
|
|
def get_object():
|
|
|
|
line = sys.__stdin__.readline()
|
|
|
|
return pyon.decode(line)
|
|
|
|
|
|
|
|
|
2014-10-05 16:25:31 +08:00
|
|
|
def put_object(obj):
|
2014-10-25 16:31:34 +08:00
|
|
|
ds = pyon.encode(obj)
|
2014-10-05 16:25:31 +08:00
|
|
|
sys.__stdout__.write(ds)
|
|
|
|
sys.__stdout__.write("\n")
|
|
|
|
sys.__stdout__.flush()
|
|
|
|
|
|
|
|
|
2015-01-07 17:50:05 +08:00
|
|
|
class ParentActionError(Exception):
|
|
|
|
pass
|
2014-12-31 17:41:22 +08:00
|
|
|
|
|
|
|
|
2015-01-07 17:50:05 +08:00
|
|
|
def make_parent_action(action, argnames, exception=ParentActionError):
|
|
|
|
argnames = argnames.split()
|
|
|
|
def parent_action(*args):
|
|
|
|
request = {"action": action}
|
|
|
|
for argname, arg in zip(argnames, args):
|
|
|
|
request[argname] = arg
|
|
|
|
put_object(request)
|
|
|
|
reply = get_object()
|
|
|
|
if reply["status"] == "ok":
|
|
|
|
return reply["data"]
|
|
|
|
else:
|
2015-01-13 17:27:36 +08:00
|
|
|
raise exception(reply["message"])
|
2015-01-07 17:50:05 +08:00
|
|
|
return parent_action
|
|
|
|
|
|
|
|
|
2015-01-12 18:51:23 +08:00
|
|
|
class ParentDDB:
|
|
|
|
request = make_parent_action("req_device", "name", KeyError)
|
|
|
|
|
|
|
|
|
|
|
|
class ParentPDB:
|
|
|
|
request = make_parent_action("req_parameter", "name", KeyError)
|
|
|
|
set = make_parent_action("set_parameter", "name value")
|
2014-12-31 17:41:22 +08:00
|
|
|
|
|
|
|
|
2015-01-14 11:37:08 +08:00
|
|
|
init_rt_results = make_parent_action("init_rt_results", "description")
|
|
|
|
update_rt_results = make_parent_action("update_rt_results", "mod")
|
2015-01-13 19:12:19 +08:00
|
|
|
|
|
|
|
|
2015-03-11 23:43:07 +08:00
|
|
|
class Watchdog:
|
|
|
|
_create = make_parent_action("create_watchdog", "t")
|
|
|
|
_delete = make_parent_action("delete_watchdog", "wid")
|
|
|
|
|
|
|
|
def __init__(self, t):
|
|
|
|
self.t = t
|
|
|
|
|
|
|
|
def __enter__(self):
|
|
|
|
self.wid = Watchdog._create(self.t)
|
|
|
|
|
|
|
|
def __exit__(self, type, value, traceback):
|
|
|
|
Watchdog._delete(self.wid)
|
|
|
|
|
|
|
|
|
2015-02-20 03:09:11 +08:00
|
|
|
class Scheduler:
|
2015-02-20 10:50:52 +08:00
|
|
|
run_queued = make_parent_action("scheduler_run_queued", "run_params")
|
2015-02-20 03:09:11 +08:00
|
|
|
cancel_queued = make_parent_action("scheduler_cancel_queued", "rid")
|
|
|
|
run_timed = make_parent_action("scheduler_run_timed",
|
2015-02-20 10:50:52 +08:00
|
|
|
"run_params next_run")
|
2015-02-20 03:09:11 +08:00
|
|
|
cancel_timed = make_parent_action("scheduler_cancel_timed", "trid")
|
2015-03-11 23:43:07 +08:00
|
|
|
watchdog = Watchdog
|
2015-02-20 03:09:11 +08:00
|
|
|
|
|
|
|
|
2015-03-08 22:43:04 +08:00
|
|
|
def get_exp(file, exp):
|
2015-01-13 19:12:19 +08:00
|
|
|
module = file_import(file)
|
2015-03-08 22:43:04 +08:00
|
|
|
if exp is None:
|
|
|
|
exps = [v for k, v in module.__dict__.items()
|
|
|
|
if is_experiment(v)]
|
|
|
|
if len(exps) != 1:
|
|
|
|
raise ValueError("Found {} experiments in module"
|
|
|
|
.format(len(exps)))
|
|
|
|
return exps[0]
|
2015-01-13 19:12:19 +08:00
|
|
|
else:
|
2015-03-08 22:43:04 +08:00
|
|
|
return getattr(module, exp)
|
2015-01-13 19:12:19 +08:00
|
|
|
|
|
|
|
|
2015-03-10 06:34:09 +08:00
|
|
|
def main():
|
|
|
|
sys.stdout = sys.stderr
|
|
|
|
|
|
|
|
start_time = None
|
|
|
|
rid = None
|
|
|
|
run_params = None
|
|
|
|
exp = None
|
|
|
|
exp_inst = None
|
2015-01-13 19:12:19 +08:00
|
|
|
|
2015-03-09 00:27:27 +08:00
|
|
|
rdb = ResultDB(init_rt_results, update_rt_results)
|
2015-01-13 19:12:19 +08:00
|
|
|
|
2015-04-05 10:41:09 +08:00
|
|
|
with DBHub(ParentDDB, ParentPDB, rdb) as dbh:
|
2015-03-10 06:34:09 +08:00
|
|
|
while True:
|
|
|
|
obj = get_object()
|
|
|
|
action = obj["action"]
|
|
|
|
if action == "prepare":
|
|
|
|
start_time = time.localtime()
|
|
|
|
rid = obj["rid"]
|
|
|
|
run_params = obj["run_params"]
|
|
|
|
exp = get_exp(run_params["file"], run_params["experiment"])
|
|
|
|
exp_inst = exp(dbh,
|
|
|
|
scheduler=Scheduler,
|
|
|
|
run_params=run_params,
|
|
|
|
**run_params["arguments"])
|
2015-03-13 22:12:55 +08:00
|
|
|
rdb.build()
|
2015-03-10 06:34:09 +08:00
|
|
|
put_object({"action": "completed"})
|
|
|
|
elif action == "run":
|
|
|
|
exp_inst.run()
|
2015-03-20 19:01:34 +08:00
|
|
|
dbh.close_devices()
|
2015-03-10 06:34:09 +08:00
|
|
|
put_object({"action": "completed"})
|
|
|
|
elif action == "analyze":
|
|
|
|
exp_inst.analyze()
|
2015-03-12 02:06:46 +08:00
|
|
|
put_object({"action": "completed"})
|
|
|
|
elif action == "write_results":
|
2015-03-10 06:34:09 +08:00
|
|
|
f = get_hdf5_output(start_time, rid, exp.__name__)
|
|
|
|
try:
|
|
|
|
rdb.write_hdf5(f)
|
|
|
|
finally:
|
|
|
|
f.close()
|
|
|
|
put_object({"action": "completed"})
|
|
|
|
elif action == "terminate":
|
|
|
|
break
|
2014-10-05 16:25:31 +08:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|