Skip to content

Commit

Permalink
Adding wait group for process after a question from one of the students.
Browse files Browse the repository at this point in the history
  • Loading branch information
cutajarj committed Jul 19, 2021
1 parent 47beeba commit b2df1e8
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions condition_variables/wait_group_process.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
from multiprocessing import Condition, Value, Process

import time


# note this is the equivalent of a waitgroup for a process instead of a thread
class WaitGroupProcess:
def __init__(self, cv, wait_count):
self.cv = cv
self.wait_count = wait_count

def add(self, count):
self.cv.acquire()
self.wait_count.value += count
self.cv.release()

def done(self):
self.cv.acquire()
if self.wait_count.value > 0:
self.wait_count.value -= 1
if self.wait_count.value == 0:
self.cv.notify_all()
self.cv.release()

def wait(self):
self.cv.acquire()
while self.wait_count.value > 0:
self.cv.wait()
self.cv.release()


def sleep_and_done(condC, wc, time_to_sleep):
wg = WaitGroupProcess(condC, wc)
time.sleep(time_to_sleep)
wg.done()
print("Process called done")


if __name__ == '__main__':
wait_count = Value('i', 0, lock=False)
cv = Condition()
wait_group_process = WaitGroupProcess(cv, wait_count)
wait_group_process.add(3)
Process(target=sleep_and_done, args=(cv, wait_count, 2)).start()
Process(target=sleep_and_done, args=(cv, wait_count, 5)).start()
Process(target=sleep_and_done, args=(cv, wait_count, 7)).start()
wait_group_process.wait()
print("All processes complete")

0 comments on commit b2df1e8

Please sign in to comment.