本文介绍了Python多个进程共享同一个对象吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现了这个为什么多个进程具有相同的python中的对象ID ,但是我不太明白这是什么意思因为两个进程都执行相同的代码",所以我尝试了一下代码,似乎输出始终是相同的.

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.py44190856964419085696➜ ~ python test2.py43428304644342830464➜ ~ python test2.py45101561604510156160➜ ~ python test2.py43299485444329948544➜ ~ python test2.py44680042244468004224➜ ~ python test2.py43266471684326647168➜ ~ python test2.py44457383684445738368➜ ~ python test2.py43889800964388980096➜ ~ python test2.py45119993604511999360➜ ~ python test2.py45628512004562851200➜ ~ python test2.py45350311684535031168➜ ~ python test2.py43144206084314420608➜ ~ python test2.py45360346884536034688

➜ ~ python test2.py44190856964419085696➜ ~ python test2.py43428304644342830464➜ ~ python test2.py45101561604510156160➜ ~ python test2.py43299485444329948544➜ ~ python test2.py44680042244468004224➜ ~ python test2.py43266471684326647168➜ ~ python test2.py44457383684445738368➜ ~ python test2.py43889800964388980096➜ ~ python test2.py45119993604511999360➜ ~ python test2.py45628512004562851200➜ ~ python test2.py45350311684535031168➜ ~ python test2.py43144206084314420608➜ ~ python test2.py45360346884536034688

我也找到了这个参考 http://code.activestate.com/lists /python-list/656748/在网络上.似乎python多个进程共享同一个对象.

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.

推荐答案

CPython中对象的id是对象内存地址,由进程本身看到..该操作系统可防止其他进程看到其他进程的内存.

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.

严重简化警告就每个进程而言,其内存空间从0开始并增加.启动并要求操作系统提供1000字节内存块的两个不同进程都将认为它们具有0-1000内存块,但实际上并没有共享内存.

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.

请参见 https://en.wikipedia.org/wiki/Virtual_address_space 简介和更好的解释.

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

这篇关于Python多个进程共享同一个对象吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 10:17