我怀疑是这样的:
@memoize
def foo():
return something_expensive
def main():
with ProcessPoolExecutor(10) as pool:
futures = {pool.submit(foo, arg): arg for arg in args}
for future in concurrent.futures.as_completed(futures):
arg = futures[future]
try:
result = future.result()
except Exception as e:
sys.stderr.write("Failed to run foo() on {}\nGot {}\n".format(arg, e))
else:
print(result)
@memoize
由于我使用的是多处理池并且进程共享不多,因此无法工作(假设是典型的基于 dict 的缓存)。至少它似乎不起作用。
在这种情况下记忆的正确方法是什么?最终,我还想将缓存腌制到磁盘并在后续运行中加载它。