【发布时间】:2014-04-24 14:26:57
【问题描述】:
我正在使用 Python 多处理,更准确地说
from multiprocessing import Pool
p = Pool(15)
args = [(df, config1), (df, config2), ...] #list of args - df is the same object in each tuple
res = p.map_async(func, args) #func is some arbitrary function
p.close()
p.join()
这种方式内存消耗很大;几乎耗尽了我所有的 RAM(此时它变得非常慢,因此使多处理变得毫无用处)。我认为问题在于df 是一个巨大的对象(一个大熊猫数据框),它会为每个进程复制。我尝试使用multiprocessing.Value 共享数据框而不复制
shared_df = multiprocessing.Value(pandas.DataFrame, df)
args = [(shared_df, config1), (shared_df, config2), ...]
(如Python multiprocessing shared memory 中的建议),但这给了我TypeError: this type has no size(与Sharing a complex object between Python processes? 相同,很遗憾我不明白答案)。
我是第一次使用多处理,也许我的理解还不够好。 multiprocessing.Value 在这种情况下实际上是正确的吗?我看到了其他建议(例如队列),但现在有点困惑。有哪些选项可以共享内存,在这种情况下哪一个最好?
【问题讨论】:
-
查看最近的相关问题:stackoverflow.com/questions/22468279/…。
-
有最近的方法可以做到这一点,还是使用
Namespace仍然是最好的方法?你是怎么解决的@Anne
标签: python pandas multiprocessing