Created
September 27, 2015 09:03
-
-
Save itolosa/f18337ba49901df3ed0f to your computer and use it in GitHub Desktop.
functional
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import threading | |
class Semaphore(object): | |
def __init__(self, sem_limit): | |
self.condition1 = threading.Condition(threading.Lock()) | |
self.sem_limit = int(sem_limit) | |
self.sem_count = self.sem_limit | |
def acquire(self): | |
self.condition1.acquire() | |
if self.sem_count == 0: | |
self.condition1.wait() | |
else: | |
self.sem_count -= 1 | |
self.condition1.release() | |
def release(self): | |
self.condition1.acquire() | |
if self.sem_count < self.sem_limit: | |
self.condition1.notify() | |
self.sem_count += 1 | |
self.condition1.release() |
Author
itolosa
commented
Sep 27, 2015
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment