The right way to limit maximum number of threads running at once?

后端 未结 7 1469
逝去的感伤
逝去的感伤 2020-11-28 03:44

I\'d like to create a program that runs multiple light threads, but limits itself to a constant, predefined number of concurrent running tasks, like this (but with no risk o

相关标签:
7条回答
  • 2020-11-28 04:40

    I ran into this same problem and spent days (2 days to be precise) getting to the correct solution using a queue. I wasted a day going down the ThreadPoolExecutor path because there is no way to limit the number of threads that thing launches! I fed it a list of 5000 files to copy and the code went non-responsive once it got up to about 1500 concurrent file copies running all at once. The max_workers parameter on the ThreadPoolExecutor only controls how many workers are spinning up threads not how many threads get spun up.

    Ok, anyway, here is a very simple example of using a Queue for this:

    import threading, time, random
    from queue import Queue
    
    jobs = Queue()
    
    def do_stuff(q):
        while not q.empty():
            value = q.get()
            time.sleep(random.randint(1, 10))
            print(value)
            q.task_done()
    
    for i in range(10):
        jobs.put(i)
    
    for i in range(3):
        worker = threading.Thread(target=do_stuff, args=(jobs,))
        worker.start()
    
    print("waiting for queue to complete", jobs.qsize(), "tasks")
    jobs.join()
    print("all done")
    
    0 讨论(0)
提交回复
热议问题