I want in the following code to limit the number of class_f
objects running at the same time. i.e. Limit the number of threads working at the same time. How can I do that ?
#!/usr/bin/env python
import random
import time
from threading import Thread
list_num = [1,2,3,4,5,6,7,8,9,10]
class class_f(Thread):
def __init__(self,x):
Thread.__init__(self)
self.x = x
def run(self):
time.sleep(random.randint(0,1))
res = self.x * self.x
print str(res)+"\n"
def main():
for num in list_num:
c=class_f(num)
c.start()
if __name__ == "__main__":
main()