Python 3 - 使用不同参数多处理同一函数

标签 python python-3.x multiprocessing python-multiprocessing

在 python 3 中,我尝试同时运行具有多个参数的同一函数。我在 Windows 7 中的 Python 3.5.2、Anaconda 4.1.1(64 位)中使用多重处理。我收到以下有关 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 通过重新导入主模块来模拟 fork 时您的代码再次执行。这就是为什么所有主模块都应该通过if __name__ == '__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__ 检查可防止您在生成工作进程时重新执行 main 函数。

关于Python 3 - 使用不同参数多处理同一函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42522113/

相关文章:

python - 为 matplotlib 文本设置固定位置

python - 使用字节键创建字典(基于字符串)

python - 将 "pairs"的列表转换为字典的字典?

python - 使用 API 或 MessageQueue 的多进程 Web 应用程序

python - 每次测试后 flask 单元测试不关闭端口

python - 如何在Python中选择其一列值包含特定字符串的行?

python - 哪种方法可以更快地替换字符串中最后一次出现的子字符串?

python - 如何使用 "{foo} {bar} {baz}"等命名占位符动态检查格式字符串

python - 仅在 numpy 中打印真正的根

python - 打破 python 多处理管理器列表