Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- build an engine
- +-- weld metal
- +-- screw bolts
- build a car
- +-- build an engine
- | +-- weld metal
- | +-- screw bolts
- +-- build a frame
- | +-- weld metal
- | +-- screw bolts
- | +-- paint
- +-- combine engine and frame
- class Task:
- def do(self):
- raise NotImplementedError('abstract method')
- class Job(Task):
- def __init__(self):
- self.tasks = []
- def do(self):
- for task in self.tasks:
- task.do()
- class Task:
- def do(self):
- raise NotImplementedError('abstract method')
- def run(self):
- if self.block:
- self.do()
- else:
- threading.Thread(target=self.do).start()
- class Task:
- def __init__(self):
- self._done = threading.Event()
- self.dependencies = []
- def wait_for_completion(self):
- self._done.wait()
- def do(self):
- raise NotImplementedError('abstract method')
- def do_async(self):
- for depends in self.dependencies:
- depends.wait_for_completion()
- self.do()
- self._done.set()
- def run(self):
- threading.Thread(target=self.do_async).start()
- class Job(Task):
- def __init__(self):
- super(Job, self).__init__()
- self.tasks = []
- def do(self):
- started_tasks = []
- for task in self.tasks:
- if task.block:
- self._wait_for_tasks(started_tasks)
- started_tasks = []
- else:
- started_tasks.append(task)
- task.run()
- if task.block:
- task.wait_for_completion()
- self._wait_for_tasks(self.tasks)
- def _wait_for_tasks(self, task_list):
- for task in task_list:
- task.wait_for_completion()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement