Python 多处理 linux windows 区别 [英] Python multiprocessing linux windows difference

查看:21
本文介绍了Python 多处理 linux windows 区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此代码在 linux 上执行但抛出 AttributeError: type object 'T' has no attribute 'val' on windows,为什么?

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 缺少 fork() 系统调用,复制当前进程.这有很多含义,包括 windows multiprocessing 文档页面中列出的那些.更具体地说:

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.

使用多处理时还有其他怪癖,所有这些都在 python 多处理指南.

There are other quirks when using multiprocessing, all of which are detailed in the python multiprocessing guidelines.

这篇关于Python 多处理 linux windows 区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆