D1457: workers: create backgroundcloser per thread

wlis (Wojciech Lis) phabricator at mercurial-scm.org
Mon Nov 20 18:37:13 UTC 2017


wlis created this revision.
Herald added a subscriber: mercurial-devel.
Herald added a reviewer: hg-reviewers.

REVISION SUMMARY
  This allows to create _backgroundfilecloser per thread.
  The threading.local() manages the distinct storage between threads and works for main thread as well (if no actuall threading is used)

TEST PLAN
  Ran pull, update, sparse commands and watched the closer threads created and destroyed in procexp.exe
  
  ran test on CentOS. No tests broken compared to the base

REPOSITORY
  rHG Mercurial

REVISION DETAIL
  https://phab.mercurial-scm.org/D1457

AFFECTED FILES
  mercurial/vfs.py

CHANGE DETAILS

diff --git a/mercurial/vfs.py b/mercurial/vfs.py
--- a/mercurial/vfs.py
+++ b/mercurial/vfs.py
@@ -269,27 +269,27 @@
         for dirpath, dirs, files in os.walk(self.join(path), onerror=onerror):
             yield (dirpath[prefixlen:], dirs, files)
 
+    threaddata = threading.local()
+
     @contextlib.contextmanager
     def backgroundclosing(self, ui, expectedcount=-1):
         """Allow files to be closed asynchronously.
 
         When this context manager is active, ``backgroundclose`` can be passed
         to ``__call__``/``open`` to result in the file possibly being closed
         asynchronously, on a background thread.
         """
-        # This is an arbitrary restriction and could be changed if we ever
-        # have a use case.
         vfs = getattr(self, 'vfs', self)
-        if getattr(vfs, '_backgroundfilecloser', None):
+        if getattr(vfs.threaddata, '_backgroundfilecloser', None):
             raise error.Abort(
-                _('can only have 1 active background file closer'))
+                _('can only have 1 active background file closer per thread'))
 
         with backgroundfilecloser(ui, expectedcount=expectedcount) as bfc:
             try:
-                vfs._backgroundfilecloser = bfc
+                vfs.threaddata._backgroundfilecloser = bfc
                 yield bfc
             finally:
-                vfs._backgroundfilecloser = None
+                vfs.threaddata._backgroundfilecloser = None
 
 class vfs(abstractvfs):
     '''Operate files relative to a base directory
@@ -414,12 +414,12 @@
             fp = checkambigatclosing(fp)
 
         if backgroundclose:
-            if not self._backgroundfilecloser:
+            if not self.threaddata._backgroundfilecloser:
                 raise error.Abort(_('backgroundclose can only be used when a '
                                   'backgroundclosing context manager is active')
                                   )
 
-            fp = delayclosedfile(fp, self._backgroundfilecloser)
+            fp = delayclosedfile(fp, self.threaddata._backgroundfilecloser)
 
         return fp
 



To: wlis, #hg-reviewers
Cc: mercurial-devel


More information about the Mercurial-devel mailing list