python - 为什么在 '__main__'导入模块不允许multiprocessig使用模块?

标签 python python-2.7 multiprocessing arcpy

我已经通过将导入移动到顶部声明解决了我的问题,但这让我想知道:为什么我不能在函数中使用在 '__main__' 中导入的模块多处理的目标?

例如:

import os
import multiprocessing as mp

def run(in_file, out_dir, out_q):
    arcpy.RaterToPolygon_conversion(in_file, out_dir, "NO_SIMPIFY", "Value")
    status = str("Done with "+os.path.basename(in_file))
    out_q.put(status, block=False)

if __name__ == '__main__':
    raw_input("Program may hang, press Enter to import ArcPy...")
    import arcpy

    q = mp.Queue()
    _file = path/to/file
    _dir = path/to/dir
    # There are actually lots of files in a loop to build
    # processes but I just do one for context here
    p = mp.Process(target=run, args=(_file, _dir, q))
    p.start()

# I do stuff with Queue below to status user

当您在 IDLE 中运行它时,它根本不会出错...只是继续执行 Queue 检查(这很好,所以不是问题)。问题是,当您在 CMD 终端(操作系统或 Python)中运行它时,它会产生 arcpy 未定义的错误!

只是一个好奇的话题。

最佳答案

类unix系统和Windows情况不同。在 unixy 系统上,multiprocessing 使用 fork 创建共享父内存空间的写时复制 View 的子进程。子级看到从父级导入的内容,包括父级在 if __name__ == "__main__": 下导入的任何内容。

在windows上,没有fork,必须要执行一个新的进程。但是简单地重新运行父进程是行不通的——它会再次运行整个程序。相反,multiprocessing 运行自己的 python 程序,该程序导入父主脚本,然后 pickle/unpickles 父对象空间的 View ,希望该 View 足以满足子进程的需求。

该程序是子进程的__main__,父脚本的__main__ 不运行。主脚本就像任何其他模块一样被导入。原因很简单:运行父 __main__ 只会再次运行完整的父程序,mp 必须避免这种情况。

这里有一个测试来说明发生了什么。名为 testmp.py 的主模块和由第一个模块导入的第二个模块 test2.py

testmp.py

import os
import multiprocessing as mp

print("importing test2")
import test2

def worker():
    print('worker pid: {}, module name: {}, file name: {}'.format(os.getpid(), 
        __name__, __file__))

if __name__ == "__main__":
    print('main pid: {}, module name: {}, file name: {}'.format(os.getpid(), 
        __name__, __file__))
    print("running process")
    proc = mp.Process(target=worker)
    proc.start()
    proc.join()

test2.py

import os

print('test2 pid: {}, module name: {}, file name: {}'.format(os.getpid(),
        __name__, __file__))

在 Linux 上运行时,test2 被导入一次,worker 在主模块中运行。

importing test2
test2 pid: 17840, module name: test2, file name: /media/td/USB20FD/tmp/test2.py
main pid: 17840, module name: __main__, file name: testmp.py
running process
worker pid: 17841, module name: __main__, file name: testmp.py

在 Windows 下,请注意“importing test2”被打印了两次 - testmp.py 运行了两次。但是“main pid”只打印了一次——它的 __main__ 没有运行。这是因为 multiprocessing 在导入过程中将模块名称更改为 __mp_main__

E:\tmp>py testmp.py
importing test2
test2 pid: 7536, module name: test2, file name: E:\tmp\test2.py
main pid: 7536, module name: __main__, file name: testmp.py
running process
importing test2
test2 pid: 7544, module name: test2, file name: E:\tmp\test2.py
worker pid: 7544, module name: __mp_main__, file name: E:\tmp\testmp.py

关于python - 为什么在 '__main__'导入模块不允许multiprocessig使用模块?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43545179/

相关文章:

python - sys.stdin 在 ctrl-d 上不关闭

python - 无法使用 Python 循环分页 API 响应

python - 为什么 children 死不了?

python - 类型错误 : cannot pickle 'weakref' object

python - 欧氏距离数学错误

python - 为没有 GUI 的 python 程序制作键盘命令

错误识别的 Python 版本

Python 2.7- statsmodels - result.conf_int()

python:如何使用unicode字符串打印变量中的字符

python - 如何在 Python Web 机器人中有效地实现多线程/多处理?