【发布时间】:2022-01-24 08:37:24
【问题描述】:
我正在 python 中尝试多处理,但似乎无法让它工作。
输入文件如下:
代码如下:
import pandas as pd
import multiprocessing
import time
import datetime
start_time = datetime.datetime.now()
df_main = []
df_main = pd.read_csv("data.csv")
df_file = []
def growth_calculator(Type):
values = [Type]
global df_temp, df_file
df_temp = df_main[df_main.Type.isin(values)]
df_temp = df_temp[['Company', 'Type']]
print(df_temp)
time.sleep(10)
if __name__ == '__main__':
multiprocessing.Process(target=growth_calculator('Quarterly'))
multiprocessing.Process(target=growth_calculator('Annual'))
multiprocessing.Process(target=growth_calculator('Monthly'))
end_time = datetime.datetime.now()
print("Time Taken -", end_time-start_time)
输出应该需要大约 10-11 秒,但需要 30 秒。所以,很明显,多处理是行不通的。
能否请您指出正确的方向?
提前致谢!
【问题讨论】:
-
并行性开销压倒任何好处的情况并不少见。如果您真的想弄清楚时间花在哪里,您可能需要添加更多调试细节(包括代表性示例数据)。
-
您在哪个平台(Linux?Windows?其他?)下运行?当您有多处理问题时,您应该始终使用平台标记您的问题。
标签: python pandas multiprocessing