更新时间:2023-11-28 21:46:34
如果您不关心项目的处理顺序,则可以尝试在内部使用set
的Queue
子类:
If you don't care about the order in which items are processed, I'd try a subclass of Queue
that uses set
internally:
class SetQueue(Queue):
def _init(self, maxsize):
self.maxsize = maxsize
self.queue = set()
def _put(self, item):
self.queue.add(item)
def _get(self):
return self.queue.pop()
正如Paul McGuire所指出的,这将允许在将重复项从待处理"集中删除但尚未添加到已处理"集中之后添加重复项.为了解决这个问题,您可以将两个集合都存储在Queue
实例中,但是由于您使用更大的集合来检查项目是否已被处理,因此您也可以返回到queue
,它将正确地订购请求.
As Paul McGuire pointed out, this would allow adding a duplicate item after it's been removed from the "to-be-processed" set and not yet added to the "processed" set. To solve this, you can store both sets in the Queue
instance, but since you are using the larger set for checking if the item has been processed, you can just as well go back to queue
which will order requests properly.
class SetQueue(Queue):
def _init(self, maxsize):
Queue._init(self, maxsize)
self.all_items = set()
def _put(self, item):
if item not in self.all_items:
Queue._put(self, item)
self.all_items.add(item)
与单独使用一个集合相比,此方法的优点是Queue
的方法是线程安全的,因此您不需要其他锁定即可检查另一个集合.
The advantage of this, as opposed to using a set separately, is that the Queue
's methods are thread-safe, so that you don't need additional locking for checking the other set.