import threading | |
import time | |
class theThread(threading.Thread): | |
def __init__(self, threadId): # From my understanding this is the only thing that runs when I call thread1 = myThread() | |
threading.Thread.__init__(self) | |
self.threadId = threadId | |
def run(self): | |
print "running task" | |
doSomething(str(self.threadId)) | |
def doSomething(threadName): | |
time.sleep(10) | |
#in reality this will do some i/o bound stuff as well as a few small cpu bound things | |
print threadName + "finished executing" | |
list = [1, 2, 3] | |
listOfThreads = [] | |
for thread in list: | |
listOfThreads.append(theThread(thread))#initialize the thread, run the __init__ statement | |
for thread in listOfThreads: | |
if (threading.activeCount()<3): #if there's 2 threads running, don't start a new one | |
thread.start() | |
else: | |
print "too many threads, we'll wait for a few seconds then start it." | |
time.sleep(5)#until we wait forr 4 seconds | |
thread.start() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment