【问题标题】:Python concurrent.futures multiple calls to imported modulesPython concurrent.futures 对导入模块的多次调用
【发布时间】: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.futuresProcessPollExecutor 来执行下面的代码。 在 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


    【解决方案1】:

    经过一番挖掘,我找到了解决问题的方法,以防万一它可能有所帮助。 与我发现的许多帖子相反,使用 if "__name__" == __main__ 保护我的代码是不够的。

    1) 我改为multiprocessing 库而不是concurrent.future,后者是前者的简化版本。 (见下面的代码)。

    2) 结果表明,在 Ipython 控制台中的 Windows 上进行多处理不是很可靠 (Python multiprocessing pool stuck)。解决多次调用import的问题 我在开头添加了这段代码:

    import sys
    sys.modules['__main__'].__file__ = 'ipython'
    

    最后,代码看起来像这样并且运行良好,(没有损坏的进程池,也没有弹出多个 GUI)。

    import pandas as pd
    import numpy as np
    import json
    
    from HAL_PROC import N_tab, N_tab_FCT, TYPE, TR, CA
    from HAL_FUNC import *
    from HAL_GRAPH import Dossier_graph, Forecast_graph
    
    from multiprocessing import Pool
    import multiprocessing as mp
    import matplotlib.backends.backend_pdf  
    
    import sys
    sys.modules['__main__'].__file__ = 'ipython'
    
    
    """ ========================================================== """
    """                   FFR +/- ME CALCULATION                   """
    """ ========================================================== """
    
    
    def main():    
        with Pool() as p:
             results = p.starmap(F,zip(N,n,f,alpha))
             results_fct = p.starmap(F,zip(N_fct,n_fct,f_fct,alpha_fct))
             p.close()
             p.join()
    
    if __name__ == "__main__":
        N_tab, N_tab_FCT = main()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-06-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多