简体   繁体   English

Python多处理从模块调用函数

[英]Python multiprocessing calling function from module

I have a my_module module with a my_func() function. 我有一个带my_func()函数的my_module模块。 When I use this function in the parent process, then everything works well (I get the correct result from this function). 当我在父进程中使用此函数时,一切都运行良好(我从此函数得到正确的结果)。 But when I call this function from child processes then I get this error: 但是当我从子进程调用此函数时,我收到此错误:

AttributeError: module 'my_module' has no attribute 'my_func'

Does anybody know what I did wrong? 有谁知道我做错了什么? Here is my template code: 这是我的模板代码:

import pandas as pd, gc, sys, multiprocessing as mp, traceback as tb
sys.path.append('c:/my_libs/')
import my_module

def mp_func(df, i):
    df['res'] = df.apply(lambda x: my_module.my_func(x, i))
    return df

def other_function(df):
    # do something
    my_list = [1,2,3] # it can change dynamically based on previous code

    df_res = pd.DataFrame()

    mp_pool = mp.Pool(processes=min(len(my_list), mp.cpu_count()))
    try:
        for i_df in [y.get() for y in [mp_pool.apply_async(mp_func\
            , [df[['my_attr']], i]) for i in my_list]]:

            df_res = df_res.append(i_df)

    except Exception as e:
        print('Something went wrong during multiprocessing:\n', e)
        print('\nFull traceback:\n', tb.format_exc())

    finally:
        mp_pool.close()
        gc.collect()

    df_res.to_csv(<...>)    

if __name__ == '__main__':
    df = pd.read_csv(<...>)
    other_function(df)

And my_module module looks like this: my_module模块看起来像这样:

def my_func(x, i):
    return x * i

Again, if I run the NOT multiprocessed version of the same code then everything works well. 同样,如果我运行相同代码的NOT多处理版本,那么一切都运行良好。 (I use python 3) (我使用python 3)

Thank you! 谢谢!

Finally I found the problem. 最后我发现了问题。 I use Spyder, and in it the default working directory was set to an other folder where I had a previous version of my module. 我使用Spyder,并在其中将默认工作目录设置为另一个文件夹,其中我有一个以前版本的模块。 This topic helped me to print out the path of my imported modules in the child processes. 主题帮助我打印出子进程中导入模块的路径。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM