Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在导入的多处理处理器子类上使用.start()时发生IOerror_Python_Windows_Multiprocessing - Fatal编程技术网

Python 在导入的多处理处理器子类上使用.start()时发生IOerror

Python 在导入的多处理处理器子类上使用.start()时发生IOerror,python,windows,multiprocessing,Python,Windows,Multiprocessing,我在Python2.7多处理(64位windows)方面遇到问题。假设我有一个文件pathfinder.py,其中包含以下代码: import multiprocessing as mp class MWE(mp.Process): def __init__(self, n): mp.Process.__init__(self) self.daemon = True self.list = [] for i in rang

我在Python2.7多处理(64位windows)方面遇到问题。假设我有一个文件
pathfinder.py
,其中包含以下代码:

import multiprocessing as mp

class MWE(mp.Process):
    def __init__(self, n):
        mp.Process.__init__(self)
        self.daemon = True
        self.list = []
        for i in range(n):
            self.list.append(i)

    def run(self):
        print "I'm running!"

if __name__=='__main__':
    n = 10000000
    mwe = MWE(n)
    mwe.start()
对于任意大的n值,此代码执行良好。但是如果我在另一个文件中导入并运行一个类实例

from pathfinder import MWE

mwe = MWE(10000)
mwe.start()
如果n>=~10000,我将得到以下回溯:

回溯(最近一次呼叫最后一次):
文件,在
mwe.start()
文件“C:\Python27\lib\multiprocessing\process.py”,第130行,在开始处
self.\u popen=popen(self)
文件“C:\Python27\lib\multiprocessing\forking.py”,第280行,在uu init中__
to_child.close()
IOError:[Errno 22]参数无效

我认为这可能是某种竞争条件错误,但使用time.sleep延迟mwe.start()似乎不会影响这种行为。有人知道为什么会发生这种情况,或者如何避免这种情况吗?

问题在于如何在Windows中使用
多处理。导入定义
流程的模块时,例如:

from pathfinder import MWE
您必须将运行的代码封装在
中,如果
块中的
名称<=。因此,请将客户端代码更改为:

from pathfinder import MWE
if __name__ == '__main__':
    mwe = MWE(10000)
    mwe.start()
    mwe.join()
(另外,请注意,您希望在某个时刻
join()
您的流程。)

查看特定于Windows的Python限制文档


有关类似问题,请参见和。

谢谢。不幸的是,在我发现这个问题的用例中,它是由导入其他在导入时执行代码的文件(设置单例等等)引起的;对Process的唯一调用在name=='main'块中,但显然问题仍然存在。我想我得重写一下。
from pathfinder import MWE
if __name__ == '__main__':
    mwe = MWE(10000)
    mwe.start()
    mwe.join()