test_thr.py 2.43 KB
Newer Older
1 2 3 4
# Very rudimentary test of thread module

# Create a bunch of threads, let each do some work, wait until all are done

Guido van Rossum's avatar
Guido van Rossum committed
5
from test_support import verbose
Guido van Rossum's avatar
Guido van Rossum committed
6
import random
7 8 9 10
import thread
import time

mutex = thread.allocate_lock()
Guido van Rossum's avatar
Guido van Rossum committed
11
rmutex = thread.allocate_lock() # for calls to random
12 13 14 15 16 17 18 19
running = 0
done = thread.allocate_lock()
done.acquire()

numtasks = 10

def task(ident):
	global running
Guido van Rossum's avatar
Guido van Rossum committed
20 21 22
	rmutex.acquire()
	delay = random.random() * numtasks
	rmutex.release()
Guido van Rossum's avatar
Guido van Rossum committed
23
	if verbose:
Guido van Rossum's avatar
Guido van Rossum committed
24
	    print 'task', ident, 'will run for', round(delay, 1), 'sec'
25
	time.sleep(delay)
Guido van Rossum's avatar
Guido van Rossum committed
26 27
	if verbose:
	    print 'task', ident, 'done'
28 29 30 31 32 33 34 35 36 37 38
	mutex.acquire()
	running = running - 1
	if running == 0:
		done.release()
	mutex.release()

next_ident = 0
def newtask():
	global next_ident, running
	mutex.acquire()
	next_ident = next_ident + 1
Guido van Rossum's avatar
Guido van Rossum committed
39 40
	if verbose:
	    print 'creating task', next_ident
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
	thread.start_new_thread(task, (next_ident,))
	running = running + 1
	mutex.release()

for i in range(numtasks):
	newtask()

print 'waiting for all tasks to complete'
done.acquire()
print 'all tasks done'

class barrier:
	def __init__(self, n):
		self.n = n
		self.waiting = 0
		self.checkin  = thread.allocate_lock()
		self.checkout = thread.allocate_lock()
		self.checkout.acquire()

	def enter(self):
		checkin, checkout = self.checkin, self.checkout

		checkin.acquire()
		self.waiting = self.waiting + 1
		if self.waiting == self.n:
			self.waiting = self.n - 1
			checkout.release()
			return
		checkin.release()

		checkout.acquire()
		self.waiting = self.waiting - 1
		if self.waiting == 0:
			checkin.release()
			return
		checkout.release()

numtrips = 3
def task2(ident):
	global running
	for i in range(numtrips):
		if ident == 0:
			# give it a good chance to enter the next
			# barrier before the others are all out
			# of the current one
			delay = 0.001
		else:
Guido van Rossum's avatar
Guido van Rossum committed
88 89 90
			rmutex.acquire()
			delay = random.random() * numtasks
			rmutex.release()
Guido van Rossum's avatar
Guido van Rossum committed
91
		if verbose:
Guido van Rossum's avatar
Guido van Rossum committed
92
		    print 'task', ident, 'will run for', round(delay, 1), 'sec'
93
		time.sleep(delay)
Guido van Rossum's avatar
Guido van Rossum committed
94 95
		if verbose:
		    print 'task', ident, 'entering barrier', i
96
		bar.enter()
Guido van Rossum's avatar
Guido van Rossum committed
97 98
		if verbose:
		    print 'task', ident, 'leaving barrier', i
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
	mutex.acquire()
	running = running - 1
	if running == 0:
		done.release()
	mutex.release()

print '\n*** Barrier Test ***'
if done.acquire(0):
	raise ValueError, "'done' should have remained acquired"
bar = barrier(numtasks)
running = numtasks
for i in range(numtasks):
	thread.start_new_thread(task2, (i,))
done.acquire()
print 'all tasks done'