【发布时间】:2020-05-15 18:10:46
【问题描述】:
在深入了解细节之前,我想提一下,已经阅读了这篇文章,问题似乎非常相似: Python current.futures import libraries multiple times (execute code in top scope multiple times)
Spyder 4.0.1 / Python 3.7.1 64 位 / Windows 10
然而,在我的情况下,采取谨慎措施保护我的顶级代码似乎不起作用。
我使用concurrent.futures 和ProcessPollExecutor 来执行下面的代码。
在 pandas 之外,numpy...代码调用了两个自定义模块:HAL_PROC 和 HAL_FUNC,包含执行下面的循环所需的类和函数。
在执行时,HAL_PROC.py 调用另一个模块,该模块生成一个 GUI,她或他可以在其中指定输入路径、文件名...这些值或然后存储在变量中。
我的问题是:当我运行代码时,无论是在 Spyder 中还是在编译成 .exe 文件时,它都会执行main() 函数,GUI 会在屏幕上生成 8 次。即使在主要部分中没有对模块或任何相关模块的调用,因为所有内容都已存储在变量中,甚至函数。
我确定我错过了有关如何正确设置我的代码以进行多处理的重要细微差别,非常感谢任何帮助。
import pandas as pd
import numpy as np
from HAL_PROC import N_tab, N_tab_FCT, TYPE, TR, CA
from HAL_FUNC import *
import multiprocessing
from concurrent.futures import ProcessPoolExecutor
alpha = 0.05
alg_type = TYPE
if alg_type == "O":
F = FFR_ME
elif alg_type == "NO":
F = FFR_ME_NO
else :
raise TypeError("Algortithm type selected does not exist. Must be 'O' or 'NO'")
def main():
""" Set the variables for the loop """
futures = []
e = ProcessPoolExecutor(8)
""" Loop over the different task summarized in the tab 'N_tab' during the MPO_PROC step. """
for task in N_tab["TASK_NUMBER"]:
""" Declare variables N, n , f based on the previous calculations """
N = int(N_tab.loc[N_tab["TASK_NUMBER"] == task, "N_Task"])
n = int(N_tab.loc[N_tab["TASK_NUMBER"] == task, "n_i"])
f = int(N_tab.loc[N_tab["TASK_NUMBER"] == task, "F"])
"""" Implement the function using the concurrent.future module for multiprocessing. """
future = e.submit(F, N, n, f, alpha)
futures.append(future)
results = [ff.result() for ff in futures]
for i in range(len(results)):
f = int(N_tab.loc[i, "F"])
N_tab.loc[i,"LBound"] = results[i][0][f]
N_tab.loc[i,"UBound"] = results[i][1][f]
N_tab.loc[i,"ME"] = (N_tab.loc[i,"UBound"] - N_tab.loc[i,"LBound"])/\
(2*N_tab.loc[i,"N_Task"])
N_tab.loc[i,"FFR"] = (N_tab.loc[i,"LBound"] + (N_tab.loc[i,"UBound"] - N_tab.loc[i,"LBound"])/2)/\
N_tab.loc[i,"N_Task"]
if __name__ == "__main__":
multiprocessing.freeze_support()
main()
【问题讨论】:
标签: python multiprocessing pysimplegui