为什么子进程在Windows上启动时却在Linux上没有启动时导入主模块?

弗兰克·德农库尔特(Franck Dernoncourt)

示例:以下代码在Ubuntu 14.04上运行良好

# some imports
import numpy as np
import glob
import sys
import multiprocessing
import os

# creating some temporary data
tmp_dir = os.path.join('tmp', 'nptest')
if not os.path.exists(tmp_dir):
    os.makedirs(tmp_dir)
    for i in range(10):
        x = np.random.rand(100, 50)
        y = np.random.rand(200, 20)
        file_path = os.path.join(tmp_dir, '%05d.npz' % i)
        np.savez_compressed(file_path, x=x, y=y)

def read_npz(path):
    data = dict(np.load(path))
    return (data['x'], data['y'])

def parallel_read(files):
    pool = multiprocessing.Pool(processes=4)
    data_list = pool.map(read_npz, files)
    return data_list

files = glob.glob(os.path.join(tmp_dir, '*.npz'))
x = parallel_read(files)
print('done')

但在Windows 7上失败,并显示以下错误消息:

    cmd = get_command_line() + [rhandle]
    pool = multiprocessing.Pool(processes=4)
  File "C:\Anaconda\lib\multiprocessing\forking.py", line 358, in get_command_line
  File "C:\Anaconda\lib\multiprocessing\__init__.py", line 232, in Pool
    return Pool(processes, initializer, initargs, maxtasksperchild)
  File "C:\Anaconda\lib\multiprocessing\pool.py", line 159, in __init__
    is not going to be frozen to produce a Windows executable.''')
RuntimeError: 
            Attempt to start a new process before the current process
            has finished its bootstrapping phase.

            This probably means that you are on Windows and you have
            forgotten to use the proper idiom in the main module:

                if __name__ == '__main__':
                    freeze_support()
                    ...

            The "freeze_support()" line can be omitted if the program
            is not going to be frozen to produce a Windows executable.
    self._repopulate_pool()
  File "C:\Anaconda\lib\multiprocessing\pool.py", line 223, in _repopulate_pool
    w.start()
  File "C:\Anaconda\lib\multiprocessing\process.py", line 130, in start
    self._popen = Popen(self)
  File "C:\Anaconda\lib\multiprocessing\forking.py", line 258, in __init__
    cmd = get_command_line() + [rhandle]
  File "C:\Anaconda\lib\multiprocessing\forking.py", line 358, in get_command_line
    is not going to be frozen to produce a Windows executable.''')
RuntimeError: 
            Attempt to start a new process before the current process
            has finished its bootstrapping phase.

            This probably means that you are on Windows and you have
            forgotten to use the proper idiom in the main module:

                if __name__ == '__main__':
                    freeze_support()
                    ...

            The "freeze_support()" line can be omitted if the program
            is not going to be frozen to produce a Windows executable.

根据我的理解,这是由于以下事实:子进程在Windows上启动时会导入主模块,而在Linux上则不会导入。可以通过放置x = parallel_read(files)主要功能来防止Windows上的问题例如:

if __name__ == '__main__':    
    x = parallel_read(files)
    print('done')

为什么子进程在Windows上启动时却在Linux上没有启动时导入主模块?

布莱克金

Windows没有fork功能。大多数其他OS都可以使用,并且在那些平台上,它用于multiprocessing启动与父进程处于相同状态的新进程。Windows必须通过其他方式设置子进程的状态,包括导入__main__模块。

请注意,如果需要,Python 3.4(及更高版本)可让您在所有操作系统上使用非分支实现。有关此功能的讨论,请参见错误跟踪器上的问题8713

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

在Mac上启动时,为什么我的JFrame会随机收缩?

从cmd行启动时,为什么运行Shiny会在Windows上启动两个Rscript进程?

为什么动态链接导入(在Windows中)总是在应用程序启动时加载?

作为脚本启动时,模块“子进程”没有属性“运行”

RaspberryPi上的Pharo:启动时找不到模块

为什么此代码在启动时没有任何输入就显示“ A”?

Windows上启动时Webots崩溃

阻止程序在Windows上启动时自动启动

为什么Docker容器在Ubuntu 12.04上没有启动脚本的情况下无法在启动时启动?

为什么我的机器在启动时没有显示任何内容?

iTunes在Windows 10上启动时自动启动

Grub在启动时没有出现

如何从Windows启动时禁用“ igfxHK模块”进程

在Windows 7上启动时无原因的CHKDSK

如何在Windows上(启动时)运行Ubuntu服务?

在Linux Mint 18.2上启动时启动numlock

为什么plymouthd在启动时崩溃?

在Windows上启动时执行Jar执行无效,没有结果

是什么导致进程在启动时启动?

VMWare上的Ubuntu在启动时冻结

为什么交换空间在启动时没有得到文件系统检查?

FreeBSD上的Wireguard在启动时停止

启动时没有 grub 菜单

Linux上启动时启动进程的方法有哪些?

为什么网络上的设备在重新启动时似乎总是具有相同的 IP,即使它们是 dhcp?

为什么 VPN 在启动时连接?

@PostConstruct 注释在方法上没有在 springboot 应用程序启动时启动

为什么我的 React Native 应用程序在启动时崩溃而没有任何错误?

为什么从 Windows shell 和 Python 子进程启动时 Git 日志命令输出不同?