Skip to content
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

can't pickle lru_cache function with loky #292

Closed
basnijholt opened this issue Sep 2, 2020 · 9 comments
Closed

can't pickle lru_cache function with loky #292

basnijholt opened this issue Sep 2, 2020 · 9 comments

Comments

@basnijholt
Copy link
Member

basnijholt commented Sep 2, 2020

The following fails:

from functools import lru_cache
import adaptive
adaptive.notebook_extension()

@lru_cache
def g(x):
    return x

def f(x):
    return g(x)

learner = adaptive.SequenceLearner(f, range(2))
runner = adaptive.Runner(learner, adaptive.SequenceLearner.done)

runner.live_info()

Related to loky issue: joblib/loky#268

This worked fine with the concurrent.futures.ProcessPoolExecutor.

@basnijholt
Copy link
Member Author

This is because of cloudpipe/cloudpickle#178
and can be reproduced with

import cloudpickle
from functools import lru_cache

@lru_cache
def g(x):
    return x

dump = cloudpickle.dumps(g)
del g
g = cloudpickle.loads(dump)
g(1)

@akhmerov
Copy link
Contributor

akhmerov commented Sep 2, 2020

What's the use case for lru_cache'd functions?

@basnijholt
Copy link
Member Author

basnijholt commented Sep 2, 2020

This occurs in some simulation software I use.

For example

@lru_cache
def make_kwant_syst():
    ...
    return syst

def f(x):
    syst = make_kwant_syst()
    return conductance(x, syst)

@akhmerov
Copy link
Contributor

akhmerov commented Sep 2, 2020

I see. In the meantime you could hack around it by making syst global:

def make_kwant_syst():
    try:
        return syst
    except NameError:
        global syst = ...
        return syst

@basnijholt
Copy link
Member Author

basnijholt commented Sep 2, 2020

Also, it seems like this is blocked until at least the release of Python 3.9: cloudpipe/cloudpickle#309 (comment).

Other alternatives include using your own memorization decorator:

def memoize(f):
    memo = {}
    def helper(x):
        if x not in memo:            
            memo[x] = f(x)
        return memo[x]
    return helper

or using the concurrent.futures.ProcessPoolExecutor.

@akhmerov
Copy link
Contributor

akhmerov commented Sep 2, 2020

Should we then close this as an upstream bug? It seems like it will require no action either way.

@basnijholt
Copy link
Member Author

I think closing it will suggest that it's fixed. I rather leave it open with the "Blocked" label attached.

@akhmerov
Copy link
Contributor

akhmerov commented Sep 2, 2020

But it's just not our bug, that is the reason to close it.

@basnijholt
Copy link
Member Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants