In my code I need to have multiple worker thread instances running in a python program. I initially create few worker thread instances(say 10) and then add them into a pool. Whenever a client requests a service, a thread should be invoked and reserved for the client. After completing the task, the thread should add back to the pool.
I have written the following code so far. But I'm not sure about how to run threads forever in the pool(they should be sleeping when inside pool), invoke and get service whenever needed and add them back to the pool(should sleep again) after processing. Any help would be appreciated.
PRED = Queue(10)
class Worker(threading.Thread):
def __init__(self, threadID, name):
threading.Thread.__init__(self)
self.threadID =threadID
self.name = name
def run(self):
print("starting " + self.name + " thread")
while True:
??
print("Exiting " + self.name + " thread")
def work():
print("working")
time.sleep(3)
- Let's say worker threads are in PRED queue.
- work() is the method which I should call to serve a client.