-
Notifications
You must be signed in to change notification settings - Fork 174
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Unable to pickle functions that are decorated with functools.lru_cache()
#178
Comments
Using dill the cache is pickled. |
Hit this as well. Wonder if it could be special-cased in cloudpickle, since it's a part of the standard library? |
@aldanor the standard pickle works,I think it's a special-cased in cloudpickle. |
@guyskk For which Python version did you test that? |
@csadorf python 3.6 |
The standard pickle does not support pickling functions defined in the |
Yes, Pickling an actual cache raises a few problems:
|
I believe we should not try to pickle the cache contents anyway, just the fact that this function is wrapped by an LRUCache instance. |
This would be very useful for me. Any idea where to start? |
I agree that just pickling the information that the function was LRU wrapped without the cache so far seems to be the way to go forward here. Is there an easy way to do this? |
I took a stab at implementing this here: #309. Unfortunately, I don't think there's a way for us to properly preserve the |
FYI you can fix this with a snippet like so: import functools
import copyreg
lru_cache_type = type(functools.lru_cache()(lambda: None))
def new_lru_cache(func, cache_kwargs):
return functools.lru_cache(**cache_kwargs)(func)
def _pickle_lru_cache(obj: lru_cache_type):
params = {}
if hasattr(obj, "cache_parameters"):
params = obj.cache_parameters()
return new_lru_cache, (obj.__wrapped__, params)
copyreg.pickle(lru_cache_type, _pickle_lru_cache) |
Good morning, I think I just encountered this issue when attempting to use Thanks! |
@garrettwrong There is a WIP-PR for this referenced earlier (#178 (comment)), but this is indeed stalled. |
Observed behavior
The cloudpickle module is unable to pickle functions that are cached via the
functools.lru_cache
decorator function.The following minimal example triggers the issue on Python 3.6.5 and cloudpickle version 0.5.3:
Resulting in the following traceback:
Versions:
$ python -V Python 3.6.5 :: Anaconda, Inc. $ python -c "import cloudpickle; print(cloudpickle.__version__)" 0.5.3
Expected behavior
The cloudpickle module should handle cached functions by either pickling or ignoring the cache or at least should fail with a descriptive error message.
The text was updated successfully, but these errors were encountered: