无法在 mod_python 下获取类的单独实例
我正在尝试在 Apache 2.2 / mod_python 3.2.8 下运行一些 python 代码。 最终,代码执行 os.fork() 并生成 2 个独立的长期运行进程。 每个进程都必须创建一个单独的类实例,以避免并行流中任何可能的冲突。
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)
这些打印输出如下所示:
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>
如您所见,我为两个子流程获得了相同的对象。 你知道为什么在 mod_python 下会这样吗?有没有办法获取单独的实例? 多谢。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
repr() 函数给出的内存位置是虚拟内存中的地址,而不是系统全局内存中的地址。 fork() 返回的每个进程都有自己的虚拟内存空间,与其他进程完全不同。 他们不共享内存。
编辑:根据下面的布莱恩评论,从技术上讲,它们确实共享内存,直到内核决定将它们隔离(当子进程写入共享内存的一部分时)。 但行为实际上是相同的。
程序的结构是相同的,因此 python 在每个进程的不同虚拟内存存储中为每个子进程的每个相同对象使用相同的虚拟内存位置。
如果你实际修改对象的内容并测试它们,你会发现即使内存位置看起来相同,但这两个是完全不同的对象,因为它们属于两个不同的进程。 实际上,您无法修改其中一个(没有某种进程间通信来调解)。
The memory location given by the
repr()
function is an address in virtual memory, not an address in the system's global memory. Each of your processes returned by fork() has its own virtual memory space which is completely distinct from other processes. They do not share memory.Edit: Per brian's comments below, technically they do share memory until the kernel decides to segregate them (when a child writes to a portion of shared memory). The behavior, though, is effectively the same.
The structure of your programs is the same, so python uses the same virtual memory location in each processes' distinct virtual memory store for each of your identical objects for each child.
If you actually modify the content of the objects and test them, you will see that even though the memory location looks the same, the two are completely distinct objects, because they belong to two distinct processes. In reality you can't modify one from the other (without some kind of interprocess communication to mediate).