I am attempting to run some python code under Apache 2.2 / mod_python 3.2.8. Eventually the code does os.fork() and spawns 2 separate lengthy-run processes. All of individuals processes needs to produce a separate demonstration of a category to be able to avoid any possible collision within 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)

Individuals print results seem 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 tell I acquired exactly the same object for sub-processes. Have you got a concept why it is going such as this under mod_python and it is possible to method of getting separate instances anyway? Thanks.

The memory location distributed by the repr() function is definitely an address in virtual memory, no address within the system's global memory. All of your processes came back by fork() features its own virtual storage that is completely distinct using their company processes. They don't share memory.

Edit: Per brian's comments below, technically they are doing share memory before the kernel decides to segregate them (whenever a child creates to some part of shared memory). The behaviour, though, is effectively exactly the same.

The dwelling of the programs is identical, so python uses exactly the same virtual memory location in every processes' distinct virtual memory store for every of the identical objects for every child.

Should you really customize the content from the objects and test them out, you will find that despite the fact that the memory location looks exactly the same, the 2 are completely distinct objects, simply because they fit in with two distinct processes. You really can't modify one in the other (without some type of interprocess communication to mediate).