Multiprocessing error: function not defined
Multiprocessing error: function not defined
以下returns"NameError: name 'times_2' is not defined",我也想不通为什么:
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: name 'get_weather' is not defined":
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)
尝试像这样使用池:
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
以下returns"NameError: name 'times_2' is not defined",我也想不通为什么:
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: name 'get_weather' is not defined":
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)
尝试像这样使用池:
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