artiq/artiq/management/worker_impl.py

47 lines
950 B
Python
Raw Normal View History

import sys
import importlib
2014-10-25 16:31:34 +08:00
from artiq.management import pyon
def import_in_folder(path, name):
try:
del sys.modules[name] # force path search
except KeyError:
pass
loader = importlib.find_loader(name, [path])
if loader is None:
raise ImportError("Could not find loader")
return loader.load_module()
def run(path, name):
module = import_in_folder(path, name)
module.main()
def put_object(obj):
2014-10-25 16:31:34 +08:00
ds = pyon.encode(obj)
sys.__stdout__.write(ds)
sys.__stdout__.write("\n")
sys.__stdout__.flush()
def main():
sys.stdout = sys.stderr
while True:
line = sys.__stdin__.readline()
2014-10-25 16:31:34 +08:00
obj = pyon.decode(line)
put_object("ack")
try:
run(**obj)
except Exception as e:
put_object({"status": "failed", "message": str(e)})
else:
put_object({"status": "ok"})
if __name__ == "__main__":
main()