经过深思熟虑,我最终编写了一个快速的自定义队列。
class PerQueue(object):
def __init__(self):
self._init()
self.all_files = set()
self.current = None
self.files = set()
self._init = False
self.cur_files = set()
def _setflag(self, value):
self._init = value
for item in self.all_files:
if item.startswith('ss'):
self.cur_files.add(item)
def _getflag(self):
return self._init
def empty(self):
n = self._empty()
return n
def pushMany(self, itemList):
for item in itemList:
self.push(item)
def push(self, item):
if not item in self.all_files and not item in self.files:
self._put(item)
def pop(self):
# I don't want errors
if not self.empty():
self.current = self._get()
self.all_files.add(self.current)
if self.init:
self.cur_files.add(self.current)
else:
self.current = None
return self.current
def _init(self):
self.files = set()
def _empty(self):
return not self.files
def _get(self):
return self.files.pop()
def _put(self, item):
self.files.add(item)
init = property(_getflag, _setflag)
这使我能够处理几个特殊条件(使用 all_files 和 cur_files)以及 init 标志。最多我们有几百个文件随时处理,所以我不担心大小限制。