Skip to content

Commit

Permalink
Expanded explanation.
Browse files Browse the repository at this point in the history
  • Loading branch information
itamarst committed Oct 26, 2021
1 parent d9d6083 commit b32bc27
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions cachecontrol/filewrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@ class CallbackFileWrapper(object):
This class uses members with a double underscore (__) leading prefix so as
not to accidentally shadow an attribute.
The data is stored in a temporary file until it is all available. As long
as the temporary files directory is disk-based (sometimes it's a
memory-backed-``tmpfs`` on Linux), data will be unloaded to disk if memory
pressure is high. For small files the disk usually won't be used at all,
it'll all be in the filesystem memory cache, so there should be no
performance impact.
"""

def __init__(self, fp, callback):
Expand Down Expand Up @@ -59,7 +66,9 @@ def _close(self):
result = b""
else:
# Return the data without actually loading it into memory,
# relying on Python's buffer API:
# relying on Python's buffer API and mmap(). mmap() just gives
# a view directly into the filesystem's memory cache, so it
# doesn't result in duplicate memory use.
self.__buf.seek(0, 0)
result = memoryview(
mmap.mmap(self.__buf.fileno(), 0, access=mmap.ACCESS_READ)
Expand All @@ -73,8 +82,8 @@ def _close(self):
# and allows the garbage collector to do it's thing normally.
self.__callback = None

# Closing the BytesIO stream releases memory. Important when caching
# big files.
# Closing the tempoary file releases memory and frees disk space.
# Important when caching big files.
self.__buf.close()

def read(self, amt=None):
Expand Down

0 comments on commit b32bc27

Please sign in to comment.