-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsubmitAndBlock.py
68 lines (58 loc) · 2 KB
/
submitAndBlock.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
from threading import Thread, Lock, Condition
import time
class Task:
def __init__(self, command, id, runTime):
self.command = command
self.id = id
self.runTime = runTime
class WorkerInterface:
def __init__(self):
self.lock = Lock()
self.condition = Condition(self.lock)
self.queue = []
self.active_tasks = 0
self.shutdown_flag = False
self.worker_thread = Thread(target=self._worker)
self.worker_thread.start()
def run_task(self, task):
try:
time.sleep(task.runTime) # Simulate task running time
print(f"Task completed: {task.id}")
except Exception as e:
print(f"Task failed: {task.id} with error {e}")
def submitWork(self, task):
with self.lock:
self.queue.append(task)
self.active_tasks += 1
self.condition.notify()
def _worker(self):
while True:
with self.lock:
while not self.queue and not self.shutdown_flag:
self.condition.wait()
if self.shutdown_flag and not self.queue:
break
task = self.queue.pop(0)
self.run_task(task)
with self.lock:
self.active_tasks -= 1
if self.active_tasks == 0:
self.condition.notify_all()
def blockUntilComplete(self):
with self.lock:
while self.active_tasks > 0:
self.condition.wait()
print("All tasks are finished")
def shutdown(self):
with self.lock:
self.shutdown_flag = True
self.condition.notify_all()
self.worker_thread.join()
# Example usage
if __name__ == '__main__':
worker_interface = WorkerInterface()
tasks = [Task("command", i, random.uniform(0.1, 0.5)) for i in range(5)]
for task in tasks:
worker_interface.submitWork(task)
worker_interface.blockUntilComplete()
worker_interface.shutdown()