summaryrefslogtreecommitdiff
path: root/Lib/test/test_thread.py
diff options
context:
space:
mode:
authorGuido van Rossum <guido@python.org>1994-04-14 20:28:41 +0000
committerGuido van Rossum <guido@python.org>1994-04-14 20:28:41 +0000
commitcc54417d1a5458304718e15379546344c17950dd (patch)
treee24ecc7d6ea6f7e511ba7279cd741554d880a294 /Lib/test/test_thread.py
parenta873fcecdfaa9b3f6493ff438371d7f233d36b67 (diff)
downloadcpython-git-cc54417d1a5458304718e15379546344c17950dd.tar.gz
Module to test threads
Diffstat (limited to 'Lib/test/test_thread.py')
-rw-r--r--Lib/test/test_thread.py41
1 files changed, 41 insertions, 0 deletions
diff --git a/Lib/test/test_thread.py b/Lib/test/test_thread.py
new file mode 100644
index 0000000000..5bc30766c3
--- /dev/null
+++ b/Lib/test/test_thread.py
@@ -0,0 +1,41 @@
+# Very rudimentary test of thread module
+
+# Create a bunch of threads, let each do some work, wait until all are done
+
+import whrandom
+import thread
+import time
+
+mutex = thread.allocate_lock()
+running = 0
+done = thread.allocate_lock()
+done.acquire()
+
+def task(ident):
+ global running
+ delay = whrandom.random() * 10
+ print 'task', ident, 'will run for', delay, 'sec'
+ time.sleep(delay)
+ print 'task', ident, 'done'
+ 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
+ print 'creating task', next_ident
+ thread.start_new_thread(task, (next_ident,))
+ running = running + 1
+ mutex.release()
+
+for i in range(10):
+ newtask()
+
+print 'waiting for all tasks to complete'
+done.acquire()
+print 'all tasks done'