annotate Lib/test/test_threading.py @ 27998:e27f58a50737 legacy-trunk

SF bug #690377: Dud footnote reference in distutils docs
author Raymond Hettinger <python@rcn.com>
date Fri, 21 Feb 2003 02:47:09 +0000
parents aa0d2d51fbde
children cc27b249755d
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
19405
9de83dda9f09 add a few test cases for threading module.
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1 # Very rudimentary test of threading module
9de83dda9f09 add a few test cases for threading module.
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
2
9de83dda9f09 add a few test cases for threading module.
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
3 # Create a bunch of threads, let each do some work, wait until all are done
9de83dda9f09 add a few test cases for threading module.
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
4
24364
aa0d2d51fbde Get rid of relative imports in all unittests. Now anything that
Barry Warsaw <barry@python.org>
parents: 19413
diff changeset
5 from test.test_support import verbose
19405
9de83dda9f09 add a few test cases for threading module.
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
6 import random
9de83dda9f09 add a few test cases for threading module.
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
7 import threading
9de83dda9f09 add a few test cases for threading module.
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
8 import time
9de83dda9f09 add a few test cases for threading module.
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
9
19413
ccc4af687cac Test failed because these was no expected-output file, but always printed
Tim Peters <tim.peters@gmail.com>
parents: 19405
diff changeset
10 # This takes about n/3 seconds to run (about n/3 clumps of tasks, times
ccc4af687cac Test failed because these was no expected-output file, but always printed
Tim Peters <tim.peters@gmail.com>
parents: 19405
diff changeset
11 # about 1 second per clump).
19405
9de83dda9f09 add a few test cases for threading module.
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
12 numtasks = 10
9de83dda9f09 add a few test cases for threading module.
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
13
9de83dda9f09 add a few test cases for threading module.
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
14 # no more than 3 of the 10 can run at once
9de83dda9f09 add a few test cases for threading module.
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
15 sema = threading.BoundedSemaphore(value=3)
9de83dda9f09 add a few test cases for threading module.
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
16 mutex = threading.RLock()
9de83dda9f09 add a few test cases for threading module.
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
17 running = 0
9de83dda9f09 add a few test cases for threading module.
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
18
9de83dda9f09 add a few test cases for threading module.
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
19 class TestThread(threading.Thread):
9de83dda9f09 add a few test cases for threading module.
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
20 def run(self):
9de83dda9f09 add a few test cases for threading module.
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
21 global running
19413
ccc4af687cac Test failed because these was no expected-output file, but always printed
Tim Peters <tim.peters@gmail.com>
parents: 19405
diff changeset
22 delay = random.random() * 2
19405
9de83dda9f09 add a few test cases for threading module.
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
23 if verbose:
19413
ccc4af687cac Test failed because these was no expected-output file, but always printed
Tim Peters <tim.peters@gmail.com>
parents: 19405
diff changeset
24 print 'task', self.getName(), 'will run for', delay, 'sec'
19405
9de83dda9f09 add a few test cases for threading module.
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
25 sema.acquire()
9de83dda9f09 add a few test cases for threading module.
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
26 mutex.acquire()
9de83dda9f09 add a few test cases for threading module.
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
27 running = running + 1
9de83dda9f09 add a few test cases for threading module.
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
28 if verbose:
9de83dda9f09 add a few test cases for threading module.
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
29 print running, 'tasks are running'
9de83dda9f09 add a few test cases for threading module.
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
30 mutex.release()
9de83dda9f09 add a few test cases for threading module.
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
31 time.sleep(delay)
9de83dda9f09 add a few test cases for threading module.
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
32 if verbose:
9de83dda9f09 add a few test cases for threading module.
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
33 print 'task', self.getName(), 'done'
9de83dda9f09 add a few test cases for threading module.
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
34 mutex.acquire()
9de83dda9f09 add a few test cases for threading module.
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
35 running = running - 1
9de83dda9f09 add a few test cases for threading module.
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
36 if verbose:
9de83dda9f09 add a few test cases for threading module.
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
37 print self.getName(), 'is finished.', running, 'tasks are running'
9de83dda9f09 add a few test cases for threading module.
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
38 mutex.release()
9de83dda9f09 add a few test cases for threading module.
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
39 sema.release()
9de83dda9f09 add a few test cases for threading module.
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
40
9de83dda9f09 add a few test cases for threading module.
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
41 threads = []
9de83dda9f09 add a few test cases for threading module.
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
42 def starttasks():
9de83dda9f09 add a few test cases for threading module.
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
43 for i in range(numtasks):
9de83dda9f09 add a few test cases for threading module.
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
44 t = TestThread(name="<thread %d>"%i)
9de83dda9f09 add a few test cases for threading module.
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
45 threads.append(t)
9de83dda9f09 add a few test cases for threading module.
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
46 t.start()
9de83dda9f09 add a few test cases for threading module.
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
47
9de83dda9f09 add a few test cases for threading module.
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
48 starttasks()
9de83dda9f09 add a few test cases for threading module.
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
49
19413
ccc4af687cac Test failed because these was no expected-output file, but always printed
Tim Peters <tim.peters@gmail.com>
parents: 19405
diff changeset
50 if verbose:
ccc4af687cac Test failed because these was no expected-output file, but always printed
Tim Peters <tim.peters@gmail.com>
parents: 19405
diff changeset
51 print 'waiting for all tasks to complete'
19405
9de83dda9f09 add a few test cases for threading module.
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
52 for t in threads:
9de83dda9f09 add a few test cases for threading module.
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
53 t.join()
19413
ccc4af687cac Test failed because these was no expected-output file, but always printed
Tim Peters <tim.peters@gmail.com>
parents: 19405
diff changeset
54 if verbose:
ccc4af687cac Test failed because these was no expected-output file, but always printed
Tim Peters <tim.peters@gmail.com>
parents: 19405
diff changeset
55 print 'all tasks done'