blob: 602ad2af90146ce76fbb7ce38abd357999e4cac4 [file] [log] [blame]
Tim Petersaa222232001-05-22 09:34:27 +00001# This is a variant of the very old (early 90's) file
2# Demo/threads/bug.py. It simply provokes a number of threads into
3# trying to import the same module "at the same time".
4# There are no pleasant failure modes -- most likely is that Python
5# complains several times about module random having no attribute
6# randrange, and then Python hangs.
7
8import thread
Thomas Wouters477c8d52006-05-27 19:21:47 +00009from test.test_support import verbose, TestSkipped, TestFailed
Tim Petersaa222232001-05-22 09:34:27 +000010
11critical_section = thread.allocate_lock()
12done = thread.allocate_lock()
13
14def task():
15 global N, critical_section, done
16 import random
17 x = random.randrange(1, 3)
18 critical_section.acquire()
19 N -= 1
Tim Peters20882dd2002-02-16 07:26:27 +000020 # Must release critical_section before releasing done, else the main
21 # thread can exit and set critical_section to None as part of global
22 # teardown; then critical_section.release() raises AttributeError.
23 finished = N == 0
Tim Petersaa222232001-05-22 09:34:27 +000024 critical_section.release()
Tim Peters20882dd2002-02-16 07:26:27 +000025 if finished:
26 done.release()
Tim Petersaa222232001-05-22 09:34:27 +000027
Thomas Wouters477c8d52006-05-27 19:21:47 +000028def test_import_hangers():
29 import sys
30 if verbose:
31 print "testing import hangers ...",
32
Thomas Wouters0e3f5912006-08-11 14:57:12 +000033 import test.threaded_import_hangers
Thomas Wouters477c8d52006-05-27 19:21:47 +000034 try:
Thomas Wouters0e3f5912006-08-11 14:57:12 +000035 if test.threaded_import_hangers.errors:
36 raise TestFailed(test.threaded_import_hangers.errors)
Thomas Wouters477c8d52006-05-27 19:21:47 +000037 elif verbose:
38 print "OK."
39 finally:
40 # In case this test is run again, make sure the helper module
41 # gets loaded from scratch again.
42 del sys.modules['test.threaded_import_hangers']
43
Tim Petersd9742212001-05-22 18:28:25 +000044# Tricky: When regrtest imports this module, the thread running regrtest
45# grabs the import lock and won't let go of it until this module returns.
46# All other threads attempting an import hang for the duration. Since
47# this test spawns threads that do little *but* import, we can't do that
48# successfully until after this module finishes importing and regrtest
49# regains control. To make this work, a special case was added to
50# regrtest to invoke a module's "test_main" function (if any) after
51# importing it.
Tim Petersaa222232001-05-22 09:34:27 +000052
Tim Petersd9742212001-05-22 18:28:25 +000053def test_main(): # magic name! see above
Tim Petersaa222232001-05-22 09:34:27 +000054 global N, done
Tim Peters69232342001-08-30 05:16:13 +000055
56 import imp
57 if imp.lock_held():
58 # This triggers on, e.g., from test import autotest.
59 raise TestSkipped("can't run when import lock is held")
60
Tim Petersaa222232001-05-22 09:34:27 +000061 done.acquire()
Tim Petersd9742212001-05-22 18:28:25 +000062 for N in (20, 50) * 3:
63 if verbose:
64 print "Trying", N, "threads ...",
Tim Petersaa222232001-05-22 09:34:27 +000065 for i in range(N):
66 thread.start_new_thread(task, ())
67 done.acquire()
Tim Petersd9742212001-05-22 18:28:25 +000068 if verbose:
69 print "OK."
Michael W. Hudsonfcc09bb2004-08-03 10:45:59 +000070 done.release()
Tim Petersaa222232001-05-22 09:34:27 +000071
Thomas Wouters477c8d52006-05-27 19:21:47 +000072 test_import_hangers()
73
Tim Petersd9742212001-05-22 18:28:25 +000074if __name__ == "__main__":
75 test_main()