]> granicus.if.org Git - python/commitdiff
Speed test_threading up from 14s to .5s, and avoid a deadlock on certain
authorJeffrey Yasskin <jyasskin@gmail.com>
Fri, 21 Mar 2008 18:48:04 +0000 (18:48 +0000)
committerJeffrey Yasskin <jyasskin@gmail.com>
Fri, 21 Mar 2008 18:48:04 +0000 (18:48 +0000)
failures. The test for enumerate-after-join is now a little less rigorous, but
the bug it references says the error happened in the first couple iterations,
so 100 iterations should still be enough.

cProfile was useful for identifying the slow tests here.

Lib/test/test_threading.py

index dc94603472b8ad23efc102f4472f8b00e6e8a064..e309a5439e4f12eccd9d0e25148d6ee9c8546457 100644 (file)
@@ -30,32 +30,28 @@ class TestThread(threading.Thread):
         self.nrunning = nrunning
 
     def run(self):
-        delay = random.random() * 2
+        delay = random.random() / 10000.0
         if verbose:
-            print 'task', self.getName(), 'will run for', delay, 'sec'
+            print 'task %s will run for %.1f usec' % (
+                self.getName(), delay * 1e6)
 
-        self.sema.acquire()
+        with self.sema:
+            with self.mutex:
+                self.nrunning.inc()
+                if verbose:
+                    print self.nrunning.get(), 'tasks are running'
+                self.testcase.assert_(self.nrunning.get() <= 3)
 
-        self.mutex.acquire()
-        self.nrunning.inc()
-        if verbose:
-            print self.nrunning.get(), 'tasks are running'
-        self.testcase.assert_(self.nrunning.get() <= 3)
-        self.mutex.release()
-
-        time.sleep(delay)
-        if verbose:
-            print 'task', self.getName(), 'done'
-
-        self.mutex.acquire()
-        self.nrunning.dec()
-        self.testcase.assert_(self.nrunning.get() >= 0)
-        if verbose:
-            print self.getName(), 'is finished.', self.nrunning.get(), \
-                  'tasks are running'
-        self.mutex.release()
+            time.sleep(delay)
+            if verbose:
+                print 'task', self.getName(), 'done'
 
-        self.sema.release()
+            with self.mutex:
+                self.nrunning.dec()
+                self.testcase.assert_(self.nrunning.get() >= 0)
+                if verbose:
+                    print '%s is finished. %d tasks are running' % (
+                        self.getName(), self.nrunning.get())
 
 class ThreadTests(unittest.TestCase):
 
@@ -218,6 +214,10 @@ class ThreadTests(unittest.TestCase):
         rc = subprocess.call([sys.executable, "-c", """if 1:
             import ctypes, sys, time, thread
 
+            # This lock is used as a simple event variable.
+            ready = thread.allocate_lock()
+            ready.acquire()
+
             # Module globals are cleared before __del__ is run
             # So we save the functions in class dict
             class C:
@@ -229,10 +229,11 @@ class ThreadTests(unittest.TestCase):
 
             def waitingThread():
                 x = C()
+                ready.release()
                 time.sleep(100)
 
             thread.start_new_thread(waitingThread, ())
-            time.sleep(1) # be sure the other thread is waiting
+            ready.acquire()  # Be sure the other thread is waiting.
             sys.exit(42)
             """])
         self.assertEqual(rc, 42)
@@ -242,9 +243,11 @@ class ThreadTests(unittest.TestCase):
         # threading.enumerate() after it has been join()ed.
         enum = threading.enumerate
         old_interval = sys.getcheckinterval()
-        sys.setcheckinterval(1)
         try:
-            for i in xrange(1, 1000):
+            for i in xrange(1, 100):
+                # Try a couple times at each thread-switching interval
+                # to get more interleavings.
+                sys.setcheckinterval(i // 5)
                 t = threading.Thread(target=lambda: None)
                 t.start()
                 t.join()