annotate Lib/test/test_thread.py @ 4566:6403c94de50d legacy-trunk

Added safeguard against failure in __del__.
author Guido van Rossum <guido@python.org>
date Wed, 27 Nov 1996 19:39:58 +0000
parents 03e097ddeca2
children 7acdf59f31c5
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1588
bcf0b8dfdae9 Module to test threads
Guido van Rossum <guido@python.org>
parents:
diff changeset
1 # Very rudimentary test of thread module
bcf0b8dfdae9 Module to test threads
Guido van Rossum <guido@python.org>
parents:
diff changeset
2
bcf0b8dfdae9 Module to test threads
Guido van Rossum <guido@python.org>
parents:
diff changeset
3 # Create a bunch of threads, let each do some work, wait until all are done
bcf0b8dfdae9 Module to test threads
Guido van Rossum <guido@python.org>
parents:
diff changeset
4
bcf0b8dfdae9 Module to test threads
Guido van Rossum <guido@python.org>
parents:
diff changeset
5 import whrandom
bcf0b8dfdae9 Module to test threads
Guido van Rossum <guido@python.org>
parents:
diff changeset
6 import thread
bcf0b8dfdae9 Module to test threads
Guido van Rossum <guido@python.org>
parents:
diff changeset
7 import time
bcf0b8dfdae9 Module to test threads
Guido van Rossum <guido@python.org>
parents:
diff changeset
8
bcf0b8dfdae9 Module to test threads
Guido van Rossum <guido@python.org>
parents:
diff changeset
9 mutex = thread.allocate_lock()
1647
03e097ddeca2 added barrier test (by Tim Peters)
Guido van Rossum <guido@python.org>
parents: 1588
diff changeset
10 whmutex = thread.allocate_lock() # for calls to whrandom
1588
bcf0b8dfdae9 Module to test threads
Guido van Rossum <guido@python.org>
parents:
diff changeset
11 running = 0
bcf0b8dfdae9 Module to test threads
Guido van Rossum <guido@python.org>
parents:
diff changeset
12 done = thread.allocate_lock()
bcf0b8dfdae9 Module to test threads
Guido van Rossum <guido@python.org>
parents:
diff changeset
13 done.acquire()
bcf0b8dfdae9 Module to test threads
Guido van Rossum <guido@python.org>
parents:
diff changeset
14
1647
03e097ddeca2 added barrier test (by Tim Peters)
Guido van Rossum <guido@python.org>
parents: 1588
diff changeset
15 numtasks = 10
03e097ddeca2 added barrier test (by Tim Peters)
Guido van Rossum <guido@python.org>
parents: 1588
diff changeset
16
1588
bcf0b8dfdae9 Module to test threads
Guido van Rossum <guido@python.org>
parents:
diff changeset
17 def task(ident):
bcf0b8dfdae9 Module to test threads
Guido van Rossum <guido@python.org>
parents:
diff changeset
18 global running
1647
03e097ddeca2 added barrier test (by Tim Peters)
Guido van Rossum <guido@python.org>
parents: 1588
diff changeset
19 whmutex.acquire()
03e097ddeca2 added barrier test (by Tim Peters)
Guido van Rossum <guido@python.org>
parents: 1588
diff changeset
20 delay = whrandom.random() * numtasks
03e097ddeca2 added barrier test (by Tim Peters)
Guido van Rossum <guido@python.org>
parents: 1588
diff changeset
21 whmutex.release()
1588
bcf0b8dfdae9 Module to test threads
Guido van Rossum <guido@python.org>
parents:
diff changeset
22 print 'task', ident, 'will run for', delay, 'sec'
bcf0b8dfdae9 Module to test threads
Guido van Rossum <guido@python.org>
parents:
diff changeset
23 time.sleep(delay)
bcf0b8dfdae9 Module to test threads
Guido van Rossum <guido@python.org>
parents:
diff changeset
24 print 'task', ident, 'done'
bcf0b8dfdae9 Module to test threads
Guido van Rossum <guido@python.org>
parents:
diff changeset
25 mutex.acquire()
bcf0b8dfdae9 Module to test threads
Guido van Rossum <guido@python.org>
parents:
diff changeset
26 running = running - 1
bcf0b8dfdae9 Module to test threads
Guido van Rossum <guido@python.org>
parents:
diff changeset
27 if running == 0:
bcf0b8dfdae9 Module to test threads
Guido van Rossum <guido@python.org>
parents:
diff changeset
28 done.release()
bcf0b8dfdae9 Module to test threads
Guido van Rossum <guido@python.org>
parents:
diff changeset
29 mutex.release()
bcf0b8dfdae9 Module to test threads
Guido van Rossum <guido@python.org>
parents:
diff changeset
30
bcf0b8dfdae9 Module to test threads
Guido van Rossum <guido@python.org>
parents:
diff changeset
31 next_ident = 0
bcf0b8dfdae9 Module to test threads
Guido van Rossum <guido@python.org>
parents:
diff changeset
32 def newtask():
bcf0b8dfdae9 Module to test threads
Guido van Rossum <guido@python.org>
parents:
diff changeset
33 global next_ident, running
bcf0b8dfdae9 Module to test threads
Guido van Rossum <guido@python.org>
parents:
diff changeset
34 mutex.acquire()
bcf0b8dfdae9 Module to test threads
Guido van Rossum <guido@python.org>
parents:
diff changeset
35 next_ident = next_ident + 1
bcf0b8dfdae9 Module to test threads
Guido van Rossum <guido@python.org>
parents:
diff changeset
36 print 'creating task', next_ident
bcf0b8dfdae9 Module to test threads
Guido van Rossum <guido@python.org>
parents:
diff changeset
37 thread.start_new_thread(task, (next_ident,))
bcf0b8dfdae9 Module to test threads
Guido van Rossum <guido@python.org>
parents:
diff changeset
38 running = running + 1
bcf0b8dfdae9 Module to test threads
Guido van Rossum <guido@python.org>
parents:
diff changeset
39 mutex.release()
bcf0b8dfdae9 Module to test threads
Guido van Rossum <guido@python.org>
parents:
diff changeset
40
1647
03e097ddeca2 added barrier test (by Tim Peters)
Guido van Rossum <guido@python.org>
parents: 1588
diff changeset
41 for i in range(numtasks):
1588
bcf0b8dfdae9 Module to test threads
Guido van Rossum <guido@python.org>
parents:
diff changeset
42 newtask()
bcf0b8dfdae9 Module to test threads
Guido van Rossum <guido@python.org>
parents:
diff changeset
43
bcf0b8dfdae9 Module to test threads
Guido van Rossum <guido@python.org>
parents:
diff changeset
44 print 'waiting for all tasks to complete'
bcf0b8dfdae9 Module to test threads
Guido van Rossum <guido@python.org>
parents:
diff changeset
45 done.acquire()
bcf0b8dfdae9 Module to test threads
Guido van Rossum <guido@python.org>
parents:
diff changeset
46 print 'all tasks done'
1647
03e097ddeca2 added barrier test (by Tim Peters)
Guido van Rossum <guido@python.org>
parents: 1588
diff changeset
47
03e097ddeca2 added barrier test (by Tim Peters)
Guido van Rossum <guido@python.org>
parents: 1588
diff changeset
48 class barrier:
03e097ddeca2 added barrier test (by Tim Peters)
Guido van Rossum <guido@python.org>
parents: 1588
diff changeset
49 def __init__(self, n):
03e097ddeca2 added barrier test (by Tim Peters)
Guido van Rossum <guido@python.org>
parents: 1588
diff changeset
50 self.n = n
03e097ddeca2 added barrier test (by Tim Peters)
Guido van Rossum <guido@python.org>
parents: 1588
diff changeset
51 self.waiting = 0
03e097ddeca2 added barrier test (by Tim Peters)
Guido van Rossum <guido@python.org>
parents: 1588
diff changeset
52 self.checkin = thread.allocate_lock()
03e097ddeca2 added barrier test (by Tim Peters)
Guido van Rossum <guido@python.org>
parents: 1588
diff changeset
53 self.checkout = thread.allocate_lock()
03e097ddeca2 added barrier test (by Tim Peters)
Guido van Rossum <guido@python.org>
parents: 1588
diff changeset
54 self.checkout.acquire()
03e097ddeca2 added barrier test (by Tim Peters)
Guido van Rossum <guido@python.org>
parents: 1588
diff changeset
55
03e097ddeca2 added barrier test (by Tim Peters)
Guido van Rossum <guido@python.org>
parents: 1588
diff changeset
56 def enter(self):
03e097ddeca2 added barrier test (by Tim Peters)
Guido van Rossum <guido@python.org>
parents: 1588
diff changeset
57 checkin, checkout = self.checkin, self.checkout
03e097ddeca2 added barrier test (by Tim Peters)
Guido van Rossum <guido@python.org>
parents: 1588
diff changeset
58
03e097ddeca2 added barrier test (by Tim Peters)
Guido van Rossum <guido@python.org>
parents: 1588
diff changeset
59 checkin.acquire()
03e097ddeca2 added barrier test (by Tim Peters)
Guido van Rossum <guido@python.org>
parents: 1588
diff changeset
60 self.waiting = self.waiting + 1
03e097ddeca2 added barrier test (by Tim Peters)
Guido van Rossum <guido@python.org>
parents: 1588
diff changeset
61 if self.waiting == self.n:
03e097ddeca2 added barrier test (by Tim Peters)
Guido van Rossum <guido@python.org>
parents: 1588
diff changeset
62 self.waiting = self.n - 1
03e097ddeca2 added barrier test (by Tim Peters)
Guido van Rossum <guido@python.org>
parents: 1588
diff changeset
63 checkout.release()
03e097ddeca2 added barrier test (by Tim Peters)
Guido van Rossum <guido@python.org>
parents: 1588
diff changeset
64 return
03e097ddeca2 added barrier test (by Tim Peters)
Guido van Rossum <guido@python.org>
parents: 1588
diff changeset
65 checkin.release()
03e097ddeca2 added barrier test (by Tim Peters)
Guido van Rossum <guido@python.org>
parents: 1588
diff changeset
66
03e097ddeca2 added barrier test (by Tim Peters)
Guido van Rossum <guido@python.org>
parents: 1588
diff changeset
67 checkout.acquire()
03e097ddeca2 added barrier test (by Tim Peters)
Guido van Rossum <guido@python.org>
parents: 1588
diff changeset
68 self.waiting = self.waiting - 1
03e097ddeca2 added barrier test (by Tim Peters)
Guido van Rossum <guido@python.org>
parents: 1588
diff changeset
69 if self.waiting == 0:
03e097ddeca2 added barrier test (by Tim Peters)
Guido van Rossum <guido@python.org>
parents: 1588
diff changeset
70 checkin.release()
03e097ddeca2 added barrier test (by Tim Peters)
Guido van Rossum <guido@python.org>
parents: 1588
diff changeset
71 return
03e097ddeca2 added barrier test (by Tim Peters)
Guido van Rossum <guido@python.org>
parents: 1588
diff changeset
72 checkout.release()
03e097ddeca2 added barrier test (by Tim Peters)
Guido van Rossum <guido@python.org>
parents: 1588
diff changeset
73
03e097ddeca2 added barrier test (by Tim Peters)
Guido van Rossum <guido@python.org>
parents: 1588
diff changeset
74 numtrips = 3
03e097ddeca2 added barrier test (by Tim Peters)
Guido van Rossum <guido@python.org>
parents: 1588
diff changeset
75 def task2(ident):
03e097ddeca2 added barrier test (by Tim Peters)
Guido van Rossum <guido@python.org>
parents: 1588
diff changeset
76 global running
03e097ddeca2 added barrier test (by Tim Peters)
Guido van Rossum <guido@python.org>
parents: 1588
diff changeset
77 for i in range(numtrips):
03e097ddeca2 added barrier test (by Tim Peters)
Guido van Rossum <guido@python.org>
parents: 1588
diff changeset
78 if ident == 0:
03e097ddeca2 added barrier test (by Tim Peters)
Guido van Rossum <guido@python.org>
parents: 1588
diff changeset
79 # give it a good chance to enter the next
03e097ddeca2 added barrier test (by Tim Peters)
Guido van Rossum <guido@python.org>
parents: 1588
diff changeset
80 # barrier before the others are all out
03e097ddeca2 added barrier test (by Tim Peters)
Guido van Rossum <guido@python.org>
parents: 1588
diff changeset
81 # of the current one
03e097ddeca2 added barrier test (by Tim Peters)
Guido van Rossum <guido@python.org>
parents: 1588
diff changeset
82 delay = 0.001
03e097ddeca2 added barrier test (by Tim Peters)
Guido van Rossum <guido@python.org>
parents: 1588
diff changeset
83 else:
03e097ddeca2 added barrier test (by Tim Peters)
Guido van Rossum <guido@python.org>
parents: 1588
diff changeset
84 whmutex.acquire()
03e097ddeca2 added barrier test (by Tim Peters)
Guido van Rossum <guido@python.org>
parents: 1588
diff changeset
85 delay = whrandom.random() * numtasks
03e097ddeca2 added barrier test (by Tim Peters)
Guido van Rossum <guido@python.org>
parents: 1588
diff changeset
86 whmutex.release()
03e097ddeca2 added barrier test (by Tim Peters)
Guido van Rossum <guido@python.org>
parents: 1588
diff changeset
87 print 'task', ident, 'will run for', delay, 'sec'
03e097ddeca2 added barrier test (by Tim Peters)
Guido van Rossum <guido@python.org>
parents: 1588
diff changeset
88 time.sleep(delay)
03e097ddeca2 added barrier test (by Tim Peters)
Guido van Rossum <guido@python.org>
parents: 1588
diff changeset
89 print 'task', ident, 'entering barrier', i
03e097ddeca2 added barrier test (by Tim Peters)
Guido van Rossum <guido@python.org>
parents: 1588
diff changeset
90 bar.enter()
03e097ddeca2 added barrier test (by Tim Peters)
Guido van Rossum <guido@python.org>
parents: 1588
diff changeset
91 print 'task', ident, 'leaving barrier', i
03e097ddeca2 added barrier test (by Tim Peters)
Guido van Rossum <guido@python.org>
parents: 1588
diff changeset
92 mutex.acquire()
03e097ddeca2 added barrier test (by Tim Peters)
Guido van Rossum <guido@python.org>
parents: 1588
diff changeset
93 running = running - 1
03e097ddeca2 added barrier test (by Tim Peters)
Guido van Rossum <guido@python.org>
parents: 1588
diff changeset
94 if running == 0:
03e097ddeca2 added barrier test (by Tim Peters)
Guido van Rossum <guido@python.org>
parents: 1588
diff changeset
95 done.release()
03e097ddeca2 added barrier test (by Tim Peters)
Guido van Rossum <guido@python.org>
parents: 1588
diff changeset
96 mutex.release()
03e097ddeca2 added barrier test (by Tim Peters)
Guido van Rossum <guido@python.org>
parents: 1588
diff changeset
97
03e097ddeca2 added barrier test (by Tim Peters)
Guido van Rossum <guido@python.org>
parents: 1588
diff changeset
98 print '\n*** Barrier Test ***'
03e097ddeca2 added barrier test (by Tim Peters)
Guido van Rossum <guido@python.org>
parents: 1588
diff changeset
99 if done.acquire(0):
03e097ddeca2 added barrier test (by Tim Peters)
Guido van Rossum <guido@python.org>
parents: 1588
diff changeset
100 raise ValueError, "'done' should have remained acquired"
03e097ddeca2 added barrier test (by Tim Peters)
Guido van Rossum <guido@python.org>
parents: 1588
diff changeset
101 bar = barrier(numtasks)
03e097ddeca2 added barrier test (by Tim Peters)
Guido van Rossum <guido@python.org>
parents: 1588
diff changeset
102 running = numtasks
03e097ddeca2 added barrier test (by Tim Peters)
Guido van Rossum <guido@python.org>
parents: 1588
diff changeset
103 for i in range(numtasks):
03e097ddeca2 added barrier test (by Tim Peters)
Guido van Rossum <guido@python.org>
parents: 1588
diff changeset
104 thread.start_new_thread(task2, (i,))
03e097ddeca2 added barrier test (by Tim Peters)
Guido van Rossum <guido@python.org>
parents: 1588
diff changeset
105 done.acquire()
03e097ddeca2 added barrier test (by Tim Peters)
Guido van Rossum <guido@python.org>
parents: 1588
diff changeset
106 print 'all tasks done'