Python多处理Linux Windows的区别 [英] Python multiprocessing linux windows difference
问题描述
此代码在linux上执行但抛出AttributeError:类型对象'T'在Windows上没有属性'val',为什么?
This code executes on linux but throws an AttributeError: type object 'T' has no attribute 'val' on windows, why?
from multiprocessing import Process
import sys
class T():
@classmethod
def init(cls, val):
cls.val = val
def f():
print(T.val)
if __name__ == '__main__':
T.init(5)
f()
p = Process(target=f, args=())
p.start()
推荐答案
Windows缺少
Windows lacks a fork()
system call, which duplicates current process. This has many implications, including those listed on the windows multiprocessing documentation page. More specifically:
请记住,如果在子进程中运行的代码尝试访问全局变量,则它看到的值(如果有)可能与Process.start时父进程中的值不同.叫.
Bear in mind that if code run in a child process tries to access a global variable, then the value it sees (if any) may not be the same as the value in the parent process at the time that Process.start was called.
在内部,python通过从头开始一个新进程并告诉它再次加载所有模块,从而在Windows上创建了一个新进程.因此,您在当前流程中所做的任何更改都将不会显示.
In internals, python creates a new process on windows by starting a new process from scratch, and telling it to load all modules again. So any change you have done in current process will not be seen.
在您的示例中,这意味着将在子进程中加载您的模块,但不会运行if __name__ == '__main__'
部分.因此T.init
将不会被调用,并且T.val
将不存在,因此会看到错误.
In your example, this means that in the child process, your module will be loaded, but the if __name__ == '__main__'
section will not be run. So T.init
will not be called, and T.val
won't exist, thus the error you see.
另一方面,在POSIX系统(包括Linux)上,进程创建使用fork,所有全局状态保持不变.该子项带有所有内容的副本,因此不必重新加载任何内容,并且将看到其T
副本及其val
副本.
On the other hand, on POSIX systems (that includes Linux), process creation uses fork, and all global state is left untouched. The child runs with a copy of everything, so it does not have to reload anything and will see its copy of T
with its copy of val
.
这也意味着在POSIX系统上,进程的创建要快得多,资源占用也要少得多,特别是因为复制"使用写时复制来避免实际复制数据的开销.
This also means that Process creation is much faster and much lighter on resources on POSIX systems, especially as the "duplication" uses copy-on-write to avoid the overhead of actually copying the data.
There are other quirks when using multiprocessing, all of which are detailed in the python multiprocessing guidelines.
这篇关于Python多处理Linux Windows的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!