为什么多处理中的新对象具有相同的ID? [英] Why do new objects in multiprocessing have the same id?
问题描述
在尝试使用多处理模块时,我试图在一个流程中创建一个新对象.但是,有些事情使我感到困惑.
I tried to create a new object in a process when using multiprocessing module. However, something confuses me.
当我使用多处理模块时,新对象的ID是相同的
When I use multiprocessing module, the id of the new object is the same
for i in range(4):
p = multiprocessing.Process(target=worker)
p.start()
def worker():
# stanford named entity tagger
st = StanfordNERTagger(model_path,stanford_ner_path)
print id(st) # all the processes print the same id
但是当我使用线程时,它们是不同的:
But when I use threading, they are different:
for i in range(4):
p = threading.Thread(target=worker)
p.start()
def worker():
# stanford named entity tagger
st = StanfordNERTagger(model_path,stanford_ner_path)
print id(st) # threads print differnt ids
我想知道为什么它们与众不同.
I am wondering why they are different.
推荐答案
id
id in CPython returns the pointer of the given object. As threads have shared address space, two different instances of an object will be allocated in two different locations returning two different ids (aka virtual address pointers).
拥有自己的地址空间的单独进程不是这种情况.偶然地,它们碰巧获得了相同的地址指针.
This is not the case for separate processes which own their own address space. By chance, they happen to get the same address pointer.
请记住,地址指针是虚拟的,因此它们表示进程地址空间本身内的偏移量.这就是为什么它们相同的原因.
Keep in mind that address pointers are virtual, therefore they represent an offset within the process address space itself. That's why they are the same.
通常最好不要依靠id()来区分对象,因为新对象可能会获得旧对象的ID,从而随着时间的推移很难跟踪它们.通常会导致棘手的错误.
It is usually better not to rely on id() for distinguishing objects, as new ones might get ids of old ones making hard to track them over time. It usually leads to tricky bugs.
这篇关于为什么多处理中的新对象具有相同的ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!