简体   繁体   English

Python Windows 和 Linux 之间的多进程差异

[英]Python Multiprocess diff between Windows and Linux

I have a script called jobrunner.py that calls class methods in main.py.我有一个名为 jobrunner.py 的脚本,它在 main.py 中调用 class 方法。 See below...见下文...

# jobrunner.py
from multiprocessing import Process
import main
from main import BBOX

def _a(arg):
    f = main.a()
    print f.run()

def _b(arg):
    p = main.b()
    print p.run()

if __name__ == '__main__':
    world = '-180,180,-90,90'
    BBOX.append(world.split(','))

    p1 = Process(target=_a, args=("1",))
    p2 = Process(target=_b, args=("1",))

    p1.start()
    p2.start()

    p1.join()
    p2.join()

Processes _a and _b are invoked without any problems on OSX and Ubuntu but when I try to run the same thing on Windows (same version of python and all), it fails saying that index is out of range.在 OSX 和 Ubuntu 上调用进程 _a 和 _b 没有任何问题,但是当我尝试在 Windows 上运行相同的东西时(相同版本的 Z23EEEB4347BDD26BFC6B7EEE 超出范围并且全部失败)。 This leads me to believe that the "global" variable BBOX is not being set or passed between modules on the Windows platform.这让我相信在 Windows 平台上的模块之间没有设置或传递“全局”变量 BBOX。 Has anyone else seen something like this and know how to fix it?有没有其他人看到过这样的事情并知道如何解决它?

Adam亚当

UPDATE: I figured it out even though it might be a total hack...See below!更新:我想通了,即使它可能是一个完全的黑客......见下文!

# jobrunner.py
from multiprocessing import Process
import main
from main import BBOX

def _a(arg):
    BBOX.append(arg) #This is the key
    f = main.a()
    print f.run()

def _b(arg):
    BBOX.append(arg) #This is the key
    p = main.b()
    print p.run()

if __name__ == '__main__':
    world = '-180,180,-90,90'
    BBOX.append(world.split(','))

    p1 = Process(target=_a, args=(BBOX[0],))
    p2 = Process(target=_b, args=(BBOX[0],))

    p1.start()
    p2.start()

    p1.join()
    p2.join()

You shouldn't expect the values of global variables that you set in the parent process to be automatically propagated to the child processes.您不应该期望您在父进程中设置的全局变量的值会自动传播到子进程。

Your code happens to work on Unix-like platforms because on those platforms multiprocessing uses fork() .您的代码恰好可以在类 Unix 平台上运行,因为在这些平台上, multiprocessing使用fork() This means that every child processes gets a copy of the parent process's address space, including all global variables.这意味着每个子进程都会获得父进程地址空间的副本,包括所有全局变量。

This isn't the case on Windows; Windows 不是这种情况; every variable from the parent process that needs to be accessed by the child has to be explicitly passed down or placed in shared memory .父进程中需要被子进程访问的每个变量都必须显式向下传递或放置在共享 memory 中

Once you do this, your code will work on both Unix and Windows.完成此操作后,您的代码将在 Unix 和 Windows 上运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM