使用 Python 的 multiprocessing.Process 类

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17172878/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 00:40:16  来源:igfitidea点击:

Using Python's multiprocessing.Process class

pythonclassmultiprocessing

提问by user2239318

This is a newbie question:

这是一个新手问题:

A class is an object, so I can create a class called pippo()and inside of this add function and parameter, I don't understand if the functions inside of pippoare executed from up to down when I assign x=pippo()or I must call them as x.dosomething()outside of pippo.

类是一个对象,这样我就可以创建一个名为类pippo()和这个附加功能和参数内,如果里面的功能我不明白,pippo从上往下执行时我给你x=pippo()或我必须叫他们为x.dosomething()境外pippo

Working with Python's multiprocessing package, is it better to define a big function and create the object using the targetargument in the call to Process(), or to create your own process class by inheriting from Processclass?

使用 Python 的 multiprocessing 包,是定义一个大函数并使用target调用中的参数创建对象Process(),还是通过从Process类继承来创建自己的进程类更好?

采纳答案by Velimir Mlaker

I often wondered why Python's doc page on multiprocessingonly shows the "functional" approach (using targetparameter). Probably because terse, succinct code snippets are best for illustration purposes. For small tasks that fit in one function, I can see how that is the preferred way, ala:

我经常想知道为什么 Python 的多处理文档页面只显示了“函数式”方法(使用target参数)。可能是因为简洁明了的代码片段最适合用于说明目的。对于适合一个功能的小任务,我可以看到这是首选方式,ala:

from multiprocessing import Process

def f():
    print('hello')

p = Process(target=f)
p.start()
p.join()

But when you need greater code organization (for complex tasks), making your own class is the way to go:

但是当你需要更好的代码组织(对于复杂的任务)时,创建你自己的类是要走的路:

from multiprocessing import Process

class P(Process):
    def __init__(self):
        super(P, self).__init__()
    def run(self):
        print('hello')

p = P()
p.start()
p.join()

Bear in mind that each spawned process is initialized with a copy of the memory footprint of the master process. And that the constructor code (i.e. stuff inside __init__()) is executed in the master process -- only code inside run()executes in separate processes.

请记住,每个生成的进程都使用主进程内存占用的副本进行初始化。并且构造函数代码(即里面的东西__init__())在主进程中run()执行——只有里面的代码在单独的进程中执行。

Therefore, if a process (master or spawned) changes it's member variable, the change will not be reflected in other processes. This, of course, is only true for bulit-in types, like bool, string, list, etc. You can however import "special" data structures from multiprocessingmodule which are then transparently shared between processes (see Sharing state between processes.) Or, you can create your own channels of IPC (inter-process communication) such as multiprocessing.Pipeand multiprocessing.Queue.

因此,如果一个进程(master 或 spawned)更改了它的成员变量,则该更改将不会反映在其他进程中。当然,这仅适用于内置类型,例如boolstringlist等。 但是,您可以从multiprocessing模块中导入“特殊”数据结构,然后在进程之间透明地共享这些数据结构(请参阅在进程之间共享状态。)或者,您可以创建您自己的 IPC(进程间通信)通道,例如multiprocessing.Pipemultiprocessing.Queue