2000-02-26 10:24:46 +00:00
|
|
|
from threading import *
|
|
|
|
|
|
|
|
|
|
class Threader:
|
|
|
|
|
"A thread generating class"
|
|
|
|
|
|
|
|
|
|
def __init__(self, num=5):
|
|
|
|
|
self.maxThreads = num
|
|
|
|
|
self.threads = []
|
|
|
|
|
|
|
|
|
|
def acquire(self):
|
|
|
|
|
"Wait until we are allowed to start a new thread"
|
|
|
|
|
while 1:
|
|
|
|
|
self.reduceThreads()
|
|
|
|
|
if len(self.threads) < self.maxThreads:
|
|
|
|
|
break
|
|
|
|
|
|
|
|
|
|
def reduceThreads(self):
|
|
|
|
|
for t in self.threads:
|
|
|
|
|
if not t.isAlive():
|
|
|
|
|
self.threads.remove(t)
|
|
|
|
|
|
|
|
|
|
def finished(self):
|
|
|
|
|
return not len(self.threads)
|
|
|
|
|
|
|
|
|
|
def finish(self):
|
|
|
|
|
self.reduceThreads()
|
2000-03-21 11:38:22 +00:00
|
|
|
# dont know how to stop a thread
|
2000-02-26 10:24:46 +00:00
|
|
|
|
|
|
|
|
def startThread(self, callable, args):
|
|
|
|
|
"Generate a new thread"
|
|
|
|
|
self.acquire()
|
|
|
|
|
t = Thread(None, callable, None, args)
|
|
|
|
|
t.start()
|
|
|
|
|
self.threads.append(t)
|