多处理错误:函数未定义

时间:2018-12-06 16:22:50

标签: python-3.x python-multiprocessing

以下内容返回“ NameError:未定义名称'times_2'”,我不知道为什么:

def pass_data(data): return times_2(data)

def times_2(data): return data*2

import multiprocessing
multiprocessing.pool = Pool()
pool.ncpus = 2
res = pool.map(pass_data, range(5))
print(res)

我实际上想做的是将一个函数应用于pandas数据框。但是,因为我不能使用lambda函数来做到这一点:

pool.map(lambda x: x.apply(get_weather, axis=1), df_split)

相反,我可以通过以下辅助方法使用它,但是会引发“ NameError:未定义名称'get_weather'”:

def get_weather(df):
    *do stuff*
    return weather_df

def pass_dataframe(df):
    return df.apply(get_weather, axis=1)

results = pool.map(pass_dataframe, df_split)

1 个答案:

答案 0 :(得分:0)

尝试像这样使用Pool:

from multiprocessing import Pool

def pass_data(data): return times_2(data)

def times_2(data): return data*2

with Pool(processes=4) as pool:
    res = pool.map(pass_data, range(5))
    print(res)

在Windows上:

from multiprocessing import Pool

def pass_data(data): return times_2(data)

def times_2(data): return data*2

if __name__ == '__main__':
    with Pool(processes=4) as pool:
        res = pool.map(pass_data, range(5))
        print(res)

请参阅文档https://docs.python.org/3/library/multiprocessing.html#multiprocessing-programming