You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

61 lines
1.6 KiB

  1. import pathlib as pl
  2. import json
  3. import importlib.machinery as impmach
  4. import multiprocessing
  5. import threading
  6. import concurrent.futures as concfut
  7. import os
  8. from . import batch
  9. from . import plan
  10. def execute(exp_file):
  11. dispatcher = load(exp_file)
  12. dispatcher.start()
  13. dispatcher.join()
  14. def load(exp_file):
  15. exp_plan = plan.Plan(exp_file, multiprocessing.Lock())
  16. with open(exp_file) as efile:
  17. exp_obj = json.loads(efile.read())
  18. exp_obj["load"] = pl.Path(exp_obj["load"])
  19. exp_mod = impmach.SourceFileLoader(exp_obj["load"].stem,
  20. str(exp_obj["load"])).load_module()
  21. return Dispatcher(exp_mod.run, exp_plan, os.cpu_count())
  22. class Dispatcher (threading.Thread):
  23. def __init__(self, exp_func, exp_plan, num_workers):
  24. threading.Thread.__init__(self)
  25. self.__exp_func = exp_func
  26. self.__plan = exp_plan
  27. self.__num_workers = num_workers
  28. self.__workers = []
  29. for i in range(self.__num_workers):
  30. self.__workers.append(multiprocessing.Process(target=self.__run_exp,
  31. args=(self.__exp_func,
  32. self.__plan)))
  33. def run(self):
  34. for worker in self.__workers:
  35. worker.start()
  36. for worker in self.__workers:
  37. worker.join()
  38. @staticmethod
  39. def __run_exp(exp_func, exp_plan):
  40. instance = exp_plan.next()
  41. while instance != None:
  42. exp_func(instance)
  43. exp_plan.done_with(instance)
  44. instance = exp_plan.next()