Last active
August 29, 2015 14:04
-
-
Save carlozamagni/29615f4627c26dbd8b84 to your computer and use it in GitHub Desktop.
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
__author__ = 'cazamagni' | |
import time | |
from threading import BoundedSemaphore, Lock, Thread | |
class Worker(Thread): | |
def __init__(self, function, semaphore): | |
Thread.__init__(self) | |
self.func = function | |
self.daemon = True | |
self.semaphore = semaphore | |
self.start() | |
def run(self): | |
try: | |
self.func() | |
except Exception, e: | |
print e | |
self.semaphore.release() | |
class WorkerPool(object): | |
def __init__(self, max_concurrence=5): | |
self.thread_pool = BoundedSemaphore(max_concurrence) | |
def add_job(self, func): | |
self.thread_pool.acquire() | |
Worker(function=func, semaphore=self.thread_pool) | |
return 'queued' | |
def my_dummy_function(): | |
print 'startup' | |
time.sleep(5) | |
print 'shutdown' | |
# TEST: max 5 concurrent threads | |
if __name__ == '__main__': | |
pool = WorkerPool() | |
while True: | |
try: | |
print pool.add_job(my_dummy_function) | |
except Exception, e: | |
print str(e) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment