2001-05-22 06:34:27 -03:00
|
|
|
# This is a variant of the very old (early 90's) file
|
|
|
|
# Demo/threads/bug.py. It simply provokes a number of threads into
|
|
|
|
# trying to import the same module "at the same time".
|
|
|
|
# There are no pleasant failure modes -- most likely is that Python
|
|
|
|
# complains several times about module random having no attribute
|
|
|
|
# randrange, and then Python hangs.
|
|
|
|
|
|
|
|
import thread
|
2001-08-29 17:26:24 -03:00
|
|
|
from test_support import verbose, TestSkipped
|
2001-05-22 06:34:27 -03:00
|
|
|
|
|
|
|
critical_section = thread.allocate_lock()
|
|
|
|
done = thread.allocate_lock()
|
|
|
|
|
|
|
|
def task():
|
|
|
|
global N, critical_section, done
|
|
|
|
import random
|
|
|
|
x = random.randrange(1, 3)
|
|
|
|
critical_section.acquire()
|
|
|
|
N -= 1
|
|
|
|
if N == 0:
|
|
|
|
done.release()
|
|
|
|
critical_section.release()
|
|
|
|
|
2001-05-22 15:28:25 -03:00
|
|
|
# Tricky: When regrtest imports this module, the thread running regrtest
|
|
|
|
# grabs the import lock and won't let go of it until this module returns.
|
|
|
|
# All other threads attempting an import hang for the duration. Since
|
|
|
|
# this test spawns threads that do little *but* import, we can't do that
|
|
|
|
# successfully until after this module finishes importing and regrtest
|
|
|
|
# regains control. To make this work, a special case was added to
|
|
|
|
# regrtest to invoke a module's "test_main" function (if any) after
|
|
|
|
# importing it.
|
2001-05-22 06:34:27 -03:00
|
|
|
|
2001-05-22 15:28:25 -03:00
|
|
|
def test_main(): # magic name! see above
|
2001-05-22 06:34:27 -03:00
|
|
|
global N, done
|
2001-08-29 17:26:24 -03:00
|
|
|
import sys
|
|
|
|
for modname in sys.modules:
|
|
|
|
if modname.find('autotest') >= 0:
|
|
|
|
raise TestSkipped("can't run from autotest")
|
2001-05-22 06:34:27 -03:00
|
|
|
done.acquire()
|
2001-05-22 15:28:25 -03:00
|
|
|
for N in (20, 50) * 3:
|
|
|
|
if verbose:
|
|
|
|
print "Trying", N, "threads ...",
|
2001-05-22 06:34:27 -03:00
|
|
|
for i in range(N):
|
|
|
|
thread.start_new_thread(task, ())
|
|
|
|
done.acquire()
|
2001-05-22 15:28:25 -03:00
|
|
|
if verbose:
|
|
|
|
print "OK."
|
2001-05-22 06:34:27 -03:00
|
|
|
|
2001-05-22 15:28:25 -03:00
|
|
|
if __name__ == "__main__":
|
|
|
|
test_main()
|