Python multiple process share the same object or not?

送分小仙女□ 提交于 2020-01-06 08:50:29

问题


I found this why multiple processes have the same object id in python, but I do not quite understand what does it mean "because both processes execute the same code", I try the code, it seems the outputs are always the same.

➜ ~ 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

I also find this refer http://code.activestate.com/lists/python-list/656748/ on web. It also seems that python multiple processes shares the same object.

Anyone could help to explain a bit further? Thanks in advance.


回答1:


The id of an object in CPython is the objects memory address as seen by the process itself. The OS prevents different processes from seeing other processes memory.

Gross simplification warning As far as each process is concerned, its memory space starts at 0 and goes up. Two different processes that start up and ask for a 1000-byte block of memory from the OS will both think they have memory block 0-1000, but they are not actually sharing memory.

See https://en.wikipedia.org/wiki/Virtual_address_space for a good intro and better explanation.



来源:https://stackoverflow.com/questions/49977765/python-multiple-process-share-the-same-object-or-not

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!