""" Running this script on the following platforms triggers a race condition in popen2 and subprocess modules: Python 2.3.3 (#1, Feb 10 2004, 17:28:29) [GCC 3.2.3 20030502 (Red Hat Linux 3.2.3-24)] on linux2 and Python 2.3.5 (#1, Mar 30 2005, 17:01:57) [GCC 3.2.3 20030502 (Red Hat Linux 3.2.3-49)] on linux2 and Python 2.4.1 (#1, Apr 15 2005, 12:58:11) [GCC 3.2.3 20030502 (Red Hat Linux 3.2.3-34)] on linux2 and Python 2.4.1 (#1, Apr 15 2005, 13:32:19) [GCC 2.95.2 19991024 (release)] on sunos5 I got no error here on this platform: Python 2.3.5 (#1, Apr 6 2005, 11:30:31) [GCC 2.96 20000731 (Red Hat Linux 7.3 2.96-113)] on linux2 """ import optparse import popen2 import sys import threading class test_popen2(threading.Thread): def __init__(self): threading.Thread.__init__(self) def run (self): for i in range(5): pipe = popen2.Popen4("ls > /dev/null") pipe.wait() class test_subprocess(threading.Thread): def __init__(self): threading.Thread.__init__(self) def run (self): import subprocess for i in range(5): pipe = subprocess.Popen("ls > /dev/null") pipe.wait() def main(argv): """ Provoke the Popen bug. """ usage = "usage: %prog" parser = optparse.OptionParser(usage) parser.add_option("-n", action="store",type="int", dest="num_threads", help="start NUM_THREADS threads", default=20) parser.add_option("-s", action="store_true", dest="subprocess", help="Use subprocess instead of popen2", default=False) (options, args) = parser.parse_args(argv) if options.subprocess: worker = test_subprocess else: worker = test_popen2 for i in range(options.num_threads): t = worker() t.start () print "Started %d threads" % options.num_threads if __name__ == '__main__': main(sys.argv[1:])