summaryrefslogtreecommitdiff
path: root/Lib/mailbox.py
diff options
context:
space:
mode:
authorAndrew M. Kuchling <amk@amk.ca>2009-05-03 02:52:20 +0000
committerAndrew M. Kuchling <amk@amk.ca>2009-05-03 02:52:20 +0000
commitfa8153401aad10baedc549a1f56a3c7c75e3cbc4 (patch)
tree0bf479093318313315d45650fab15483487c607a /Lib/mailbox.py
parenta9f48a0d4f0bad22275b5feb78f63a8a8f00a6f8 (diff)
downloadcpython-git-fa8153401aad10baedc549a1f56a3c7c75e3cbc4.tar.gz
Merged revisions 72213 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk ........ r72213 | andrew.kuchling | 2009-05-02 15:17:28 -0400 (Sat, 02 May 2009) | 3 lines #1607951: Make mailbox.Maildir re-read the directories less frequently. This is done by recording the current time -1sec, and not re-reading unless the directory mod. times are >= the recorded time. ........
Diffstat (limited to 'Lib/mailbox.py')
-rwxr-xr-xLib/mailbox.py28
1 files changed, 24 insertions, 4 deletions
diff --git a/Lib/mailbox.py b/Lib/mailbox.py
index 909285bd98..85e3ab1f8d 100755
--- a/Lib/mailbox.py
+++ b/Lib/mailbox.py
@@ -233,6 +233,7 @@ class Maildir(Mailbox):
else:
raise NoSuchMailboxError(self._path)
self._toc = {}
+ self._last_read = None # Records last time we read cur/new
def add(self, message):
"""Add message and return assigned key."""
@@ -457,16 +458,35 @@ class Maildir(Mailbox):
def _refresh(self):
"""Update table of contents mapping."""
+ new_mtime = os.path.getmtime(os.path.join(self._path, 'new'))
+ cur_mtime = os.path.getmtime(os.path.join(self._path, 'cur'))
+
+ if (self._last_read is not None and
+ new_mtime <= self._last_read and cur_mtime <= self._last_read):
+ return
+
self._toc = {}
- for subdir in ('new', 'cur'):
- subdir_path = os.path.join(self._path, subdir)
- for entry in os.listdir(subdir_path):
- p = os.path.join(subdir_path, entry)
+ def update_dir (subdir):
+ path = os.path.join(self._path, subdir)
+ for entry in os.listdir(path):
+ p = os.path.join(path, entry)
if os.path.isdir(p):
continue
uniq = entry.split(self.colon)[0]
self._toc[uniq] = os.path.join(subdir, entry)
+ update_dir('new')
+ update_dir('cur')
+
+ # We record the current time - 1sec so that, if _refresh() is called
+ # again in the same second, we will always re-read the mailbox
+ # just in case it's been modified. (os.path.mtime() only has
+ # 1sec resolution.) This results in a few unnecessary re-reads
+ # when _refresh() is called multiple times in the same second,
+ # but once the clock ticks over, we will only re-read as needed.
+ now = int(time.time() - 1)
+ self._last_read = time.time() - 1
+
def _lookup(self, key):
"""Use TOC to return subpath for given key, or raise a KeyError."""
try: