(test_thread.py): modifications to quiet it up when not running as a script.

(testall.py): added test_thread to the list of regression tests.
This commit is contained in:
Barry Warsaw 1996-12-16 23:42:35 +00:00
parent 63a0c376ea
commit af0a1a6530
2 changed files with 17 additions and 6 deletions

View File

@ -6,6 +6,10 @@ import whrandom
import thread import thread
import time import time
verbose = 0
if __name__ == '__main__':
verbose = 1
mutex = thread.allocate_lock() mutex = thread.allocate_lock()
whmutex = thread.allocate_lock() # for calls to whrandom whmutex = thread.allocate_lock() # for calls to whrandom
running = 0 running = 0
@ -19,9 +23,11 @@ def task(ident):
whmutex.acquire() whmutex.acquire()
delay = whrandom.random() * numtasks delay = whrandom.random() * numtasks
whmutex.release() whmutex.release()
print 'task', ident, 'will run for', delay, 'sec' if verbose:
print 'task', ident, 'will run for', delay, 'sec'
time.sleep(delay) time.sleep(delay)
print 'task', ident, 'done' if verbose:
print 'task', ident, 'done'
mutex.acquire() mutex.acquire()
running = running - 1 running = running - 1
if running == 0: if running == 0:
@ -33,7 +39,8 @@ def newtask():
global next_ident, running global next_ident, running
mutex.acquire() mutex.acquire()
next_ident = next_ident + 1 next_ident = next_ident + 1
print 'creating task', next_ident if verbose:
print 'creating task', next_ident
thread.start_new_thread(task, (next_ident,)) thread.start_new_thread(task, (next_ident,))
running = running + 1 running = running + 1
mutex.release() mutex.release()
@ -84,11 +91,14 @@ def task2(ident):
whmutex.acquire() whmutex.acquire()
delay = whrandom.random() * numtasks delay = whrandom.random() * numtasks
whmutex.release() whmutex.release()
print 'task', ident, 'will run for', delay, 'sec' if verbose:
print 'task', ident, 'will run for', delay, 'sec'
time.sleep(delay) time.sleep(delay)
print 'task', ident, 'entering barrier', i if verbose:
print 'task', ident, 'entering barrier', i
bar.enter() bar.enter()
print 'task', ident, 'leaving barrier', i if verbose:
print 'task', ident, 'leaving barrier', i
mutex.acquire() mutex.acquire()
running = running - 1 running = running - 1
if running == 0: if running == 0:

View File

@ -24,6 +24,7 @@ tests = ['test_grammar',
'test_struct', 'test_struct',
'test_errno', 'test_errno',
'test_dl', 'test_dl',
'test_thread',
] ]
if __name__ == '__main__': if __name__ == '__main__':