I like the simplicity. I definitely get the payoff for standalone Python scripts, where once the script errors out the memory is cleared. But do you see a similar payoff for Jupyter notebooks (or similar)?
I think the marginal gain would be a lot less for Jupyter notebooks, but I've definitely rerun individual cells and wasted time there before. I think it could help if you forget to save the output of a function within a single cell like this: 1. print(f(x)) # -> check what happened 2. out = f(x) # -> turns out we want to save this, so we have to wait again
Show HN: File-based cache for slow Python functions
31–40 of 60 posts
Re: Show HN: File-based cache for slow Python functions
#32I have extensively used https://pypi.org/project/diskcache/ . Is there a reason you decided to make an in house solution?
I'm also unsure if Diskcache supports ignoring certain fields in the function call.
Re: Show HN: File-based cache for slow Python functions
#33Earlier quoted context omitted.
I was curious to see an alternative to this, but how is this an alternative? You're saying I can implement my own caching of function calls that invalidates when the arguments or source code change...? These feel like entirely separate layers. Did I miss where diskcache does this stuff?
https://grantjenks.com/docs/diskcache/api.html#diskcache.Cac... Decorator to wrap callable with memoizing function using cache. Repeated calls with the same arguments will lookup result in cache and avoid function evaluation.
Re: Show HN: File-based cache for slow Python functions
#34If you aren’t caching LLM functions during development, then you’re an even greater glutton for punishment than the normal engineer. My local file cache Python decorator also allows the decorator to define the hash manually, either by the decorator’s parameter function call that plucks a value from the cached function params, or by calling a global function from anywhere with any arbitrary value. What’s cool about ca…
Re: Show HN: File-based cache for slow Python functions
#35If you aren’t caching LLM functions during development, then you’re an even greater glutton for punishment than the normal engineer. My local file cache Python decorator also allows the decorator to define the hash manually, either by the decorator’s parameter function call that plucks a value from the cached function params, or by calling a global function from anywhere with any arbitrary value. What’s cool about ca…
Re: Show HN: File-based cache for slow Python functions
#36I recently wrote a version of this that I use in my projects, some things I do differently that you may or may not care about: - from your code it seems you're not sorting kwargs, I would strongly recommend sorting them so that whether you call f(a=1, b=2) or f(b=2, a=1) the cache key is the same - I use inspect.signature to convert all args to kwargs, this way it doesn't matter how a function gets called, the cache…
Re: Show HN: File-based cache for slow Python functions
#37I recently wrote a version of this that I use in my projects, some things I do differently that you may or may not care about: - from your code it seems you're not sorting kwargs, I would strongly recommend sorting them so that whether you call f(a=1, b=2) or f(b=2, a=1) the cache key is the same - I use inspect.signature to convert all args to kwargs, this way it doesn't matter how a function gets called, the cache…
These tips make sense, we often use named args in our function calls (not using them has caused so many bugs), but we don't really enforce the order. Copilot doesn't always get it right either.
By moving inspect.getsource out of the wrapper, do you mean initializing it when the module is imported? I'm curious how that improves performance.
Re: Show HN: File-based cache for slow Python functions
#38def hash_code(code): return hashlib.md5(code.encode()).hexdigest() Be warned. The above function is used as part of the hash. The ostensible purpose is to prevent using cached values of functions who's code has changed, but it does not handle dependencies of that function.
How do you suggest one might fix that issue? Also pin the cache to a hash of all dependency versions? And then if one minor update And let's say the dependency did change, but it's generally inert (more error handling around edge cases, for example), how do you factor that in? Blow up the whole cache? Your example isn't really a problem with OPs utility, but a specific example of a broader dependency management probl…
Re: Show HN: File-based cache for slow Python functions
#39Earlier quoted context omitted.
I think the marginal gain would be a lot less for Jupyter notebooks, but I've definitely rerun individual cells and wasted time there before. I think it could help if you forget to save the output of a function within a single cell like this: 1. print(f(x)) # -> check what happened 2. out = f(x) # -> turns out we want to save this, so we have to wait again
FWIW There is a built-in cache system for r markdown documents. I'm not up to speed on their exact implementation but I have found it useful. https://bookdown.org/yihui/rmarkdown-cookbook/cache.html
Re: Show HN: File-based cache for slow Python functions
#40Earlier quoted context omitted.
How it fares with several millions of cached objects?
Being sqlite backed, it's really fast and threadsafe, the cache is shared safely between all threads or processes. It's a very mature library, too, nice and polished, I've never once experienced a bug with it.
Ours is more meant for single-process scripts like an LLM workflow.