How to make worker threads quit after work is finished in a multithreaded producer-consumer pattern?

后端 未结 4 1625
时光说笑
时光说笑 2021-02-05 12:56

I am trying to implement a multithreaded producer-consumer pattern using Queue.Queue in Python 2.7. I am trying to figure out how to make the consumers, i.e. the worker threads,

4条回答
  •  孤独总比滥情好
    2021-02-05 13:31

    Don't call it a special case for a task.

    Use an Event instead, with non-blocking implementation for your workers.

    stopping = threading.Event()
    
    def worker(n, q, timeout=1):
        # run until the master thread indicates we're done
        while not stopping.is_set():
            try:
                # don't block indefinitely so we can return to the top
                # of the loop and check the stopping event
                data = q.get(True, timeout)
            # raised by q.get if we reach the timeout on an empty queue
            except queue.Empty:
                continue
            q.task_done()
    
    def master():
        ...
    
        print 'waiting for workers to finish'
        q.join()
        stopping.set()
        print 'done'
    

提交回复
热议问题