Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python multithreaded memoization

Python multithreaded memoization, is it possible? If so, how?

like image 543
user578086 Avatar asked Dec 17 '25 16:12

user578086


1 Answers

Sure it's possible. In fact, I think that the straightforward single-threaded implementation should work just fine, assuming that it is acceptable that some redundant calculations may be performed in the case when the same function is called with the same parameters in parallel.

For an illustration of the scenario, your memoization procedure will probably look something like this:

if args not in cache:
    cache[args] = func(args)
return cache[args]

If two threads hit this spot at the same time with the same args, they may both invoke func(args) in parallel, while it would be more efficient to invoke just one instance of the calculation and for the other one to wait until the first one completes. This may not be too much of a problem for you. If it is, a solution using locks (from the threading module) to block threads with matching arguments should be quite straightforward.

like image 190
Gintautas Miliauskas Avatar answered Dec 19 '25 04:12

Gintautas Miliauskas



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!