1 # 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.
9 from test
.test_support
import verbose
, TestSkipped
11 critical_section
= thread
.allocate_lock()
12 done
= thread
.allocate_lock()
15 global N
, critical_section
, done
17 x
= random
.randrange(1, 3)
18 critical_section
.acquire()
20 # 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.
24 critical_section
.release()
28 # Tricky: When regrtest imports this module, the thread running regrtest
29 # grabs the import lock and won't let go of it until this module returns.
30 # All other threads attempting an import hang for the duration. Since
31 # this test spawns threads that do little *but* import, we can't do that
32 # successfully until after this module finishes importing and regrtest
33 # regains control. To make this work, a special case was added to
34 # regrtest to invoke a module's "test_main" function (if any) after
37 def test_main(): # magic name! see above
42 # This triggers on, e.g., from test import autotest.
43 raise TestSkipped("can't run when import lock is held")
46 for N
in (20, 50) * 3:
48 print "Trying", N
, "threads ...",
50 thread
.start_new_thread(task
, ())
55 if __name__
== "__main__":