2

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.

2
  • Which OS do you run on? Commented Apr 23, 2018 at 9:39
  • I am using Mac OS, I think two process should have two virtual address space as well, but I confused when I run the code in the two links. The program produce two processes, I thought the each process should have its own copy of the object, rather than use the same object. Commented Apr 23, 2018 at 11:43

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Thank your for your answer, I think each process has their own memory as well. But when I run the python code in the above two links, the program produces two process, and try to manage the same object, it seems both process deal with the same objects. I am really confuse about this, since I think each process should copy the objects to their own memory space, and deal with two different objects.
I think twice, you mean the ID of an object maybe seem the same in multiple processes, but they are different and isolated objects by the OS. right?
Yes, just because the id of the object is the same in *two different python processes", does not mean they are the same object. Processes do not/cannot share memory unless you set up a special shared memory area specifically for that purpose, which I assume you are not doing.
Thanks for your help, I will try some python code to test this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.