On Thu, Dec 2, 2010 at 10:59 AM, Chris Rebert <c...@rebertia.com> wrote: > On Wed, Dec 1, 2010 at 4:49 PM, David Brown <dmlb2...@gmail.com> wrote: >> So I'm not subscribed to python-list but would like to get an answer >> to my question. I've made a small test program that dumps a >> RuntimeError and I'd like to know why. >> >> $ python test2.py >> doing stuff >> Traceback (most recent call last): >> File "test2.py", line 3, in <module> >> import test >> RuntimeError: not holding the import lock >> child 3693 exited with 256 >> $ >> >> Here's the setup, there's two files test.py and test2.py >> >> $ cat test.py >> #!/usr/bin/python >> >> import os, sys >> >> if os.fork() == 0: >> print "doing stuff" >> sys.exit() >> >> print "child %d exited with %d" % os.wait()
Assuming you actually want to fork and do something inside the forked child process, the correct implementation is this: $ cat test.py #!/usr/bin/env python import os from time import sleep def main(): print "parent %d started" % os.getpid() if os.fork() == 0: print "doing stuff" sleep(5) print "done" raise SystemExit, 0 print "child %d exited with %d" % os.wait() if __name__ == "__main__": main() cheers James -- -- James Mills -- -- "Problems are solved by method" -- http://mail.python.org/mailman/listinfo/python-list