Clear all items from the queue

前端 未结 3 1956
无人及你
无人及你 2020-12-13 03:48

How can I clear a queue. For example I have datas in a queue, but for some reason I don\'t need the existing data, and just want to clear the queue.

Is there any wa

3条回答
  •  死守一世寂寞
    2020-12-13 04:20

    You just can not clear the queue, because every put also add the unfinished_tasks member. The join method depends on this value. And all_tasks_done needs to be notified also.

    q.mutex.acquire()
    q.queue.clear()
    q.all_tasks_done.notify_all()
    q.unfinished_tasks = 0
    q.mutex.release()
    

    or in decent way, use get and task_done pair to safely clear the tasks.

    while not q.empty():
        try:
            q.get(False)
        except Empty:
            continue
        q.task_done()
    

    or just create a new Queue and delete old one.

提交回复
热议问题