How to share a cache between multiple processes?

后端 未结 2 1001
一向
一向 2020-12-29 10:36

I\'m using a LRU cache to speed up some rather heavy duty processing. It works well and speeds things up considerably. However...

When I multiprocess, each process

2条回答
  •  感动是毒
    2020-12-29 11:16

    I believe you can use a Manager to share a dict between processes. That should in theory let you use the same cache for all functions.

    However, I think a saner logic would be to have one process that responds to queries by looking them up in the cache, and if they are not present then delegating the work to a subprocess, and caching the result before returning it. You could easily do that with

    with concurrent.futures.ProcessPoolExecutor() as e:
        @functools.lru_cache
        def work(*args, **kwargs):
            return e.submit(slow_work, *args, **kwargs)
    

    Note that work will return Future objects, which the consumer will have to wait on. The lru_cache will cache the future objects so they will be returned automatically; I believe you can access their data more than once but can't test it right now.

    If you're not using Python 3, you'll have to install backported versions of concurrent.futures and functools.lru_cache.

提交回复
热议问题