Skip to content

Instantly share code, notes, and snippets.

@pteichman pteichman/waitgroup.py
Last active Nov 25, 2019

Embed
What would you like to do?
Python WaitGroup (like Go sync.WaitGroup)
import threading # :(
class WaitGroup(object):
"""WaitGroup is like Go sync.WaitGroup.
Without all the useful corner cases.
"""
def __init__(self):
self.count = 0
self.cv = threading.Condition()
def add(self, n):
self.cv.acquire()
self.count += n
self.cv.release()
def done(self):
self.cv.acquire()
self.count -= 1
if self.count == 0:
self.cv.notify_all()
self.cv.release()
def wait(self):
self.cv.acquire()
while self.count > 0:
self.cv.wait()
self.cv.release()
@LukasKiederle

This comment has been minimized.

Copy link

LukasKiederle commented Nov 25, 2019

Nice job. I just needed that :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.