class Queue(queue.Queue):
'''
A custom queue subclass that provides a :meth:`clear` method.
'''
def clear(self):
'''
Clears all items from the queue.
'''
with self.mutex:
unfinished = self.unfinished_tasks - len(self.queue)
if unfinished <= 0:
if unfinished < 0:
raise ValueError('task_done() called too many times')
self.all_tasks_done.notify_all()
self.unfinished_tasks = unfinished
self.queue.clear()
self.not_full.notify_all()
编辑 为了简洁明了,我省略了线程安全的问题,但是@D and是非常正确的,下面的更好。
您只是不能清除队列,因为每次放置也会添加未完成的任务成员。 join方法取决于此值。 所有的任务也需要通知。
或者以体面的方式,使用get和task完成对安全地清除任务。
或者创建一个新队列并删除旧队列。
这对我来说似乎很好。如果我错过了重要的事情,我欢迎你的评论/补充。
相关问题 更多 >
编程相关推荐