def worker(queue):
    for cmd in iter(queue.get, None):
        subprocess.check_call(cmd, stdout=outputfile, stderr=subprocess.STDOUT)
q = Queue()
threads = [Thread(target=worker, args=(q,)) for _ in range(limit)]
for t in threads:
    t.daemon = True
t.start()
for cmd in commands:
    q.put_nowait(cmd)
for _ in threads:
    q.put(None)
for t in threads:
    t.join()
Control the number of subprocesses using to call external commands in python