清除队列中的所有项目
我怎样才能清除队列。例如,我在队列中有数据,但由于某种原因我不需要现有数据,只想清除队列。
有什么办法吗?这行得通吗:
oldQueue = Queue.Queue()
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 way? Will this work:
oldQueue = Queue.Queue()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
编辑
为了清楚和简洁起见,我省略了线程安全的问题,但@Dan D 是相当正确的,以下更好。
EDIT
I omitted the issue of thread safety for clarity and brevity, but @Dan D is quite correct, the following is better.
您只是无法清除队列,因为每个 put 还会添加 unfinished_tasks 成员。
连接方法取决于该值。
并且 all_tasks_done 也需要被通知。
或者以适当的方式,使用 get 和 task_done 对来安全地清除任务。
或者只是创建一个新队列并删除旧队列。
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.
or in decent way, use get and task_done pair to safely clear the tasks.
or just create a new Queue and delete old one.
这对我来说似乎做得很好。我欢迎评论/补充,以防我错过任何重要的内容。
This seems to do it pretty well for me. I welcome comments/additions in case I missed anything important.
仅使用公共 API:
Using only the public API:
如果您只想要一个空队列,并且不关心旧队列的垃圾收集,只需使用相同的句柄实例化一个新队列即可。例如,
现在为了使该队列为空,您可以简单地编写以下内容。
此时您应该有一个空队列。
If you just want an empty queue, and don't care about garbage collection of the older queue, simply instantiate a new queue using the same handle. For example,
Now in order to make this queue empty, you can simply write the following.
And at this point you should have an empty queue.