python threading: max number of threads to run
Question: let's say i have something similar to:

    
    
    def worker(name):
        time.sleep(10)
        print name
        return
    
    thrs = []
    for i in range(1000):
        t1 = threading.Thread(target=worker, args=(i,))
        thrs.append(t1)
    
    for t in thrs:
        t.start()
    

Is there way to specify how many threads can run in parallel? in the above
case, all 1000 will run in parallel


Answer: This can be done using
[`multiprocessing.dummy`](https://docs.python.org/2/library/multiprocessing.html#module-
multiprocessing.dummy) which provides a threaded version of the
[`multiprocessing`](https://docs.python.org/2/library/multiprocessing.html)
api.

    
    
    from multiprocessing.dummy import Pool
    
    pool = Pool(10)
    result = pool.map(worker, range(1000))
    

In python 3,
[`concurrent.futures.ThreadPoolExecutor`](https://docs.python.org/3/library/concurrent.futures.html#concurrent.futures.ThreadPoolExecutor)
usually provides a nicer interface.

