diff Lib/test/test_threading.py @ 45996:376b5cb8a014

Merged revisions 61687-61688,61696,61700,61704-61705,61707-61709,61711-61712,61714-61716,61718-61722 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r61687 | jeffrey.yasskin | 2008-03-21 06:02:44 +0100 (Fri, 21 Mar 2008) | 2 lines Speed up test_signal from ~24s to 4s by avoiding nearly all of the sleep calls. ........ r61688 | jeffrey.yasskin | 2008-03-21 06:51:37 +0100 (Fri, 21 Mar 2008) | 5 lines Try to fix test_signal breakages on Linux due to r61687. It appears that at least two of the linux build bots aren't leaving zombie processes around for os.waitpid to wait for, causing ECHILD errors. This would be a symptom of a bug somewhere, but probably not in signal itself. ........ r61696 | georg.brandl | 2008-03-21 15:32:33 +0100 (Fri, 21 Mar 2008) | 2 lines Mark the descitems in the tutorial as "noindex" so that :meth: cross-refs don't link to them. ........ r61700 | georg.brandl | 2008-03-21 18:19:29 +0100 (Fri, 21 Mar 2008) | 2 lines Fix markup. ........ r61704 | jeffrey.yasskin | 2008-03-21 19:25:06 +0100 (Fri, 21 Mar 2008) | 3 lines Try to fix test_signal on FreeBSD. I'm assuming that os.kill is failing to raise a signal, but switching to subprocess makes the code cleaner anyway. ........ r61705 | jeffrey.yasskin | 2008-03-21 19:48:04 +0100 (Fri, 21 Mar 2008) | 7 lines Speed test_threading up from 14s to .5s, and avoid a deadlock on certain 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. ........ r61707 | georg.brandl | 2008-03-21 20:14:38 +0100 (Fri, 21 Mar 2008) | 2 lines Fix a code block in __future__ docs. ........ r61708 | georg.brandl | 2008-03-21 20:20:21 +0100 (Fri, 21 Mar 2008) | 2 lines Add docs for __func__ and __self__ on methods. ........ r61709 | georg.brandl | 2008-03-21 20:37:57 +0100 (Fri, 21 Mar 2008) | 2 lines Add docs for print_function and future_builtins. Fixes #2442. ........ r61711 | georg.brandl | 2008-03-21 20:54:00 +0100 (Fri, 21 Mar 2008) | 2 lines #2136: allow single quotes in realm spec. ........ r61712 | georg.brandl | 2008-03-21 21:01:51 +0100 (Fri, 21 Mar 2008) | 3 lines Issue #2432: give DictReader the dialect and line_num attributes advertised in the docs. ........ r61714 | georg.brandl | 2008-03-21 21:11:46 +0100 (Fri, 21 Mar 2008) | 2 lines #2358: add py3k warning to sys.exc_clear(). ........ r61715 | georg.brandl | 2008-03-21 21:21:46 +0100 (Fri, 21 Mar 2008) | 2 lines #2346/#2347: add py3k warning for __methods__ and __members__. Patch by Jack Diederich. ........ r61716 | georg.brandl | 2008-03-21 21:38:24 +0100 (Fri, 21 Mar 2008) | 2 lines #2348: add py3k warning for file.softspace. ........ r61718 | georg.brandl | 2008-03-21 21:55:20 +0100 (Fri, 21 Mar 2008) | 2 lines #2160: document PyImport_GetImporter. ........ r61719 | georg.brandl | 2008-03-21 21:55:51 +0100 (Fri, 21 Mar 2008) | 2 lines Update doc ACKS. ........ r61720 | steven.bethard | 2008-03-21 22:01:18 +0100 (Fri, 21 Mar 2008) | 1 line Replace hack in regrtest.py with use of sys.py3kwarning. ........ r61721 | georg.brandl | 2008-03-21 22:05:03 +0100 (Fri, 21 Mar 2008) | 2 lines Add missing versionadded tag. ........ r61722 | christian.heimes | 2008-03-22 00:49:44 +0100 (Sat, 22 Mar 2008) | 3 lines Applied patch #1657 epoll and kqueue wrappers for the select module The patch adds wrappers for the Linux epoll syscalls and the BSD kqueue syscalls. Thanks to Thomas Herve and the Twisted people for their support and help. TODO: Finish documentation documentation ........
author Christian Heimes <christian@cheimes.de>
date Sat, 22 Mar 2008 00:47:35 +0000
parents 48488fa50729
children 848160d10780
line wrap: on
line diff
--- a/Lib/test/test_threading.py	Sat Mar 22 00:35:10 2008 +0000
+++ b/Lib/test/test_threading.py	Sat Mar 22 00:47:35 2008 +0000
@@ -30,32 +30,26 @@
         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')
 
-        self.sema.acquire()
-
-        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()
+        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)
 
-        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()
-
-        self.sema.release()
+            time.sleep(delay)
+            if verbose:
+                print('task', self.getName(), 'done')
+            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 +212,10 @@
         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 +227,11 @@
 
             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,7 +241,6 @@
         # threading.enumerate() after it has been join()ed.
         enum = threading.enumerate
         old_interval = sys.getcheckinterval()
-        sys.setcheckinterval(1)
         try:
             for i in range(1, 1000):
                 t = threading.Thread(target=lambda: None)