I'm trying to run some python code under Apache 2.2 / mod_python 3.2.8. Eventually the code does os.fork() and spawns 2 separate long-run processes. Each of those processes has to create a separate instance of a class in order to avoid any possible collision in the parallel flow.
class Foo(object):
pass
kidprocs = []
for kid in ('kid1', 'kid2'):
pid = os.fork()
if pid:
# parent
kidprocs.append(pid)
time.sleep(5)
else:
# child
fooobj = Foo()
print "Starting %s in sub-process %s" % (kid, os.getpid())
print "Kid fooobj: %s" % repr(fooobj)
os._exit(0)
for kidproc in kidprocs:
os.waitpid(kidproc, 0)
Those print outputs look like this:
Starting kid1 in sub-process 20906
foo obj: <__main__.Foo instance at 0xb7da5fec>
Starting kid2 in sub-process 20909
foo obj: <__main__.Foo instance at 0xb7da5fec>
As you can see I got the same object for both sub-processes. Do you have an idea why it's going like this under mod_python and is there a way to get separate instances anyway? Thanks a lot.