def __init__(self, dbfile, use_mmap=True, basepos=0):
self._file = dbfile
self.is_closed = False
# Seek to the end to get total file size (to check if mmap is OK)
dbfile.seek(0, os.SEEK_END)
filesize = self._file.tell()
dbfile.seek(basepos)
self._diroffset = self._file.read_long()
self._dirlength = self._file.read_int()
self._file.seek(self._diroffset)
self._dir = self._file.read_pickle()
self._options = self._file.read_pickle()
self._locks = {}
self._source = None
use_mmap = (
use_mmap
and hasattr(self._file, "fileno") # check file is a real file
and filesize < sys.maxsize # check fit on 32-bit Python
)
if mmap and use_mmap:
# Try to open the entire segment as a memory-mapped object
try:
fileno = self._file.fileno()
self._source = mmap.mmap(fileno, 0, access=mmap.ACCESS_READ)
except (mmap.error, OSError):
e = sys.exc_info()[1]
# If we got an error because there wasn't enough memory to
# open the map, ignore it and fall through, we'll just use the
# (slower) "sub-file" implementation
if e.errno == errno.ENOMEM:
pass
else:
raise
else:
# If that worked, we can close the file handle we were given
self._file.close()
self._file = None
评论列表
文章目录