Python 3 - 多处理相同函数但参数不同

Python 3 - Multiprocessing same function with different args

在 python 3 中,我试图 运行 同一函数同时具有多个参数。我在 Python 3.5.2、Anaconda 4.1.1(64 位)、Windows 7 中使用多处理。我收到以下关于 spawn.py 的错误:

An attempt has been made to start a new process before the current process has finished its bootstrapping phase.

This probably means that you are not using fork to start your child processes 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 an executable.

我的代码是:

from multiprocessing import Process

#Function to be run
def savedatetime(password,rangestart,rangeend):
    print((password,rangestart,rangeend))

# Inputs to the function
passwords=['p0','p1','p2']
ranges=[(1,10),(10,20),(20,30)]

# Creating the list of processes to append
fileind=0
proc=[]
for rangeind in range(0,len(passwords)):    
    password=passwords[fileind]
    rangestart=ranges[fileind][0]
    rangeend=ranges[fileind][1]
    p = Process(target=savedatetime,args=(password,rangestart,rangeend))            
    proc.append(p)
    fileind=fileind+1
    # running sequentially to check that the function works
    savedatetime(password,rangestart,rangeend)

print(proc)

# Attempting to run simultaneously. This is where the error occurs:
for p in proc:
    p.start()
    p.join()

能否请您帮我修复我的代码,以便同时使用同一函数的多个实例 运行?谢谢!

当 Windows 通过重新导入您的主模块来模拟分叉时,您需要一个看门人测试来防止您的代码再次执行。这就是为什么 所有 主模块应该通过 if __name__ == '__main__': 网守检查来控制它们的 "main-like" 行为。

最简单的解决方案是获取所有 "main" 代码,将其缩进一层,然后定义一个包含它的名为 main 的函数。

main 函数为:

def main():

    # Inputs to the function
    passwords=['p0','p1','p2']
    ranges=[(1,10),(10,20),(20,30)]

    # Creating the list of processes to append
    ... omitted for brevity ...

    # Attempting to run simultaneously. This is where the error occurs:
    for p in proc:
        p.start()

    # Join after all processes started, so you actually get parallelism
    for p in proc:
        p.join()

然后只需将以下内容添加到文件末尾:

if __name__ == '__main__':
    main()

__name__ 检查可防止在生成工作进程时重新执行主函数。