Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9253211
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T11:09:38+00:00 2026-06-18T11:09:38+00:00

Is the cache in a standard memoize decorator process-safe? For example, suppose I define

  • 0

Is the cache in a standard memoize decorator process-safe?

For example, suppose I define the following decorator:

import functools

def memoize(func):
    cache = {}
    @functools.wraps(func)
    def memoized(*args):
        result = None
        if args in cache:
            result = cache[args]
        else:
            result = func(*args)
            cache[args] = result
        return result
    return memoized

and suppose I am trying to use it to speed up computation of a recursive function, say:

@memoize
def fib(n):
    result = 1
    if n > 1:
        result = fib(n-1) + fib(n-2)
    return result

Now I wonder if two processes calculating fib() could ever clash? For example:

if __name__ == "__main__":
    from multiprocessing import Process
    p1 = Process(target=fib, args=(19,))
    p2 = Process(target=fib, args=(23,))
    p1.start()
    p2.start()
    p1.join()
    p2.join()

My first thought was that the cache is saved in the context of fib, so it is
shared between the processes and that could lead to race conditions. But then,
I think that the worst that could happen is that they would both think that, say,
fib(17) has not been calculated, and will both go ahead and calculated it in
parallel and store the same result one after the other- not ideal,
but not horrible, I guess. But I still wonder if there is a way to do it in a process-safe way.

EDIT: I added a print statement in each of the branches of memoized(),
and it seems that each process re-calculates all the fib values in the cache.
Perhaps the cache is not shared, after all? If it is not shared, I wounder
if there is a process-safe way to share it (to save some more computations).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T11:09:39+00:00Added an answer on June 18, 2026 at 11:09 am

    By default, multiprocess programs in Python share very little between processes. The few things that are shared are pickled, which comes with some limitations of its own. The fib function in your example is nominally shared, but pickle stores functions by name, not by value. That is why its cache doesn’t get shared.

    If you want to have a synchronized cache for your memoize decorator, you’ll need to add synchronization to it, such as a multiprocessing.Queue or multiprocessing.Array. This may be slower than simply letting each process recalculate the values though, since it introduces a lot of overhead as the processes pass the updates back and forth.

    Alternatively, if you don’t need your separate processes to be tightly synchronized while they’re running, you could come up with a method of passing the cache to and from the processes when they start and stop (e.g. using an extra argument and return value), so that sequential calls could benefit from the memoization, even if parallel calls do not.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The standard example for implementing LRU Cache in Java points to the example depot
I'm using the standard authentication form. It's clean method looks like this : def
the following code yield a standard animation when user selects between tabs on sdk2.0
When using a cache manifest file within ASP.NET can I just add a standard
Does enabling cache affect dynamic content? For example, on one of my PHP sites,
The standard .NET 4.0 cache cannot be shared among multiple servers.I need share cache
I'm using the second level cache provider SysCache2 with Fluent NHibernate, using a standard
I have the following table of counters: CREATE TABLE cache ( key text PRIMARY
I'm working on building out a standard set of configurations for our cache clusters
So, I was looking over my standard cache utility when preparing to unit test

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.