r47245 - some lockfile improvements

hawkowl-TA+aISz0psMTMxyoc4vAAJOcrHinNvQL0E9HWUfgJXw@public.gmane.org Thu, 14 Apr 2016 21:03:20 -0600 (MDT)
Newsgroups gmane.comp.python.twisted.commits
Message-ID <[email protected]>
Author: hawkowl
Date: Thu Apr 14 21:03:16 2016
New Revision: 47245

Modified:
   branches/moar-windows-8025-7/twisted/python/lockfile.py
   branches/moar-windows-8025-7/twisted/test/test_lockfile.py

Log:
some lockfile improvements

Modified: branches/moar-windows-8025-7/twisted/python/lockfile.py
==============================================================================
--- branches/moar-windows-8025-7/twisted/python/lockfile.py	(original)
+++ branches/moar-windows-8025-7/twisted/python/lockfile.py	Thu Apr 14 21:03:16 2016
@@ -66,6 +66,19 @@
     _open = open
     from time import sleep as _sleep
 
+    # Python 3 has no 'commit' flag for fopen, so when making the lockfile on
+    # it we must let Windows catch up... we do this by looping and reading the
+    # file, hoping to get the correct value. It sucks, but, what can you do?
+    # Locks are global state, and as we all know, global state is BAD and EVIL.
+    # What is a reasonable time to wait? Well, sleeping for a long period may
+    # waste time, but waiting for a small (0.001s) time takes far longer on
+    # Python 3.5 than the 0.001s you'd expect. 10 seconds seems like a
+    # reasonable amount of time, assuming that file I/O on Windows can be
+    # deathly slow.
+    # Therefore, 100 iterations of 0.1 second seems to work well enough.
+    _iterations = 100
+    _sleepTime = 0.1
+
     def symlink(value, filename):
         """
         Write a file at C{filename} with the contents of C{value}. See the
@@ -90,11 +103,7 @@
         if _PY3:
             readValue = ""
             iterations = 0
-            # Python 3 has no 'commit' flag for fopen, so let Windows catch
-            # up... we do this by looping and reading the file, hoping to get
-            # the correct value. It sucks, but, what can you do? Locks are
-            # global state, and as we all know, global state is BAD and EVIL.
-            # NOT EVEN ONCE - Amber
+
             while readValue != value:
                 with _open(newvalname, "r") as f:
                     readValue = f.read()
@@ -102,25 +111,19 @@
                 if readValue != value:
                     iterations += 1
 
-                    # What is a reasonable number here? Well, you give an inch,
-                    # and Windows takes a mile. Sleeping for a lot of time may
-                    # waste time, but waiting for a small (0.001s) time takes
-                    # far longer on 3.5. than the 0.001s you'd expect.
-                    # 10 seconds seems like a reasonable amount of time,
-                    # assuming that file I/O on Windows can be deathly slow.
-                    if iterations > 100:
+                    if iterations > _iterations:
                         try:
                             # Try and remove the failed lock. We have given up
-                            # at  this point, so if we can't remove it, we
+                            # at this point, so if we can't remove it, we
                             # can't really try much.
                             os.remove(newvalname)
                         except:
                             pass
                         # We ought to play sad_trombone.mp3 here. Give up and
                         # throw an exception.
-                        raise RuntimeError("Unable to get a lock.")
+                        raise OSError(errno.ETIME, "Unable to get a lock.")
 
-                    _sleep(0.1)
+                    _sleep(_sleepTime)
 
         try:
             rename(newlinkname, filename)

Modified: branches/moar-windows-8025-7/twisted/test/test_lockfile.py
==============================================================================
--- branches/moar-windows-8025-7/twisted/test/test_lockfile.py	(original)
+++ branches/moar-windows-8025-7/twisted/test/test_lockfile.py	Thu Apr 14 21:03:16 2016
@@ -134,11 +134,11 @@
 
         self.patch(lockfile, '_open', FakeOpen)
         self.patch(lockfile, '_sleep', fakeSleep)
-        self.assertRaises(RuntimeError, lockfile.symlink, name, 'data')
+        self.assertRaises(OSError, lockfile.symlink, name, 'data')
         self.assertEqual(round(sum(sleptFor)), 10.0)
     if not (platform.isWindows() and _PY3):
         test_symlinkLockTimeoutWindows.skip = (
-            "The interesting(tm) symlink timeout support is only needed on "
+            "The interesting symlink timeout support is only needed on "
             "Windows on Python 3.")