代码之家  ›  专栏  ›  技术社区  ›  cosz3

Python多进程是否共享同一对象?

  •  2
  • cosz3  · 技术社区  · 6 年前

    我找到了这个 why multiple processes have the same object id in python ,但我不太明白“因为两个进程执行相同的代码”是什么意思,我尝试了代码,似乎输出总是一样的。

    ➜ ~ python test2.py 4419085696 4419085696 ➜ ~ python test2.py 4342830464 4342830464 ➜ ~ python test2.py 4510156160 4510156160 ➜ ~ python test2.py 4329948544 4329948544 ➜ ~ python test2.py 4468004224 4468004224 ➜ ~ python test2.py 4326647168 4326647168 ➜ ~ python test2.py 4445738368 4445738368 ➜ ~ python test2.py 4388980096 4388980096 ➜ ~ python test2.py 4511999360 4511999360 ➜ ~ python test2.py 4562851200 4562851200 ➜ ~ python test2.py 4535031168 4535031168 ➜ ~ python test2.py 4314420608 4314420608 ➜ ~ python test2.py 4536034688 4536034688

    我还发现这是指 http://code.activestate.com/lists/python-list/656748/ 在web上。python多个进程似乎共享同一个对象。

    有人能帮我进一步解释一下吗?提前谢谢。

    1 回复  |  直到 6 年前
        1
  •  4
  •   Tom Dalton    6 年前

    这个 id CPython中对象的内存地址 从流程本身来看 。操作系统防止不同进程看到其他进程内存。

    粗略简化警告 就每个进程而言,它的内存空间从0开始,然后上升。启动并请求操作系统提供1000字节内存块的两个不同进程都会认为它们的内存块为0-1000,但它们实际上并没有共享内存。

    看见 https://en.wikipedia.org/wiki/Virtual_address_space 为了更好的介绍和解释。