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

Pickle doesn't work on julia modules #32

Open
msundvick opened this issue Jul 21, 2021 · 7 comments
Open

Pickle doesn't work on julia modules #32

msundvick opened this issue Jul 21, 2021 · 7 comments
Labels
bug Something isn't working

Comments

@msundvick
Copy link

Bit of a strange request, but there is a use case. A minimal example is:

import pickle
from juliacall import Base
pickle.dumps(Base)
# Traceback (most recent call last):
#   File "<stdin>", line 1, in <module>
# TypeError: cannot pickle 'juliacall.ModuleValue' object

This would be useful for defining functions that are passed around in Python. I initially ran into this when using ray, so a slightly more complex example that has the same behaviour:

import ray
ray.init()
from juliacall import Base
@ray.remote
def v(x):
    Base.println(x)
x = ray.get(v.remote(1))

I had a look at PyCall as well, but they've got a similar issue here JuliaPy/PyCall.jl#863, with a few comments on how to fix it.

Any ideas? I'll try to have a look into this further, but any help getting this working would be appreciated.

@msundvick
Copy link
Author

Oh duh, there's a workaround for my particular case. Since ray is spawning processes, and juliacall is inserting the module into the process during the init step, something like this works fine:

import ray
ray.init()
@ray.remote
def v():
    from juliacall import Base
    return Base.seval("1+1")

print(ray.get(v.remote()))

I could see this being a bit painful for other cases though, so I'll leave the issue open for now.

@cjdoris
Copy link
Collaborator

cjdoris commented Jul 21, 2021

This package is currently being rewritten from scratch, and as it happens I recently added support for pickle (and Serialization on the Julia end).

I haven't tested it out much, and will probably hang with any cyclic objects, but hopefully it will pickle modules ok.

Try it out by installing PythonCall#master and reinstalling juliacall from there. Let me know how you get on.

@cjdoris
Copy link
Collaborator

cjdoris commented Jul 21, 2021

PS it's cool you're using this with Ray, what are you planning to do?

@msundvick
Copy link
Author

Oh nice! This does solve the pickle issue, something like this works just fine

import pickle
from juliacall import Base
a = pickle.loads(pickle.dumps(Base))
print(a.rand())

Unfortunately, the way distributed computing works in ray seems to be interfering with reloading the shared library? On master, my original code for ray segfaults. The workaround of explicity importing the module inside the actor is still fine though.

The whole point of this is we've already got a bunch of code in python for doing a scientific simulation (for racing yacht's, to be precise). So we've already done some code optimization, parallelizing with ray. There's still some bottlenecks though, and it would be nice to switch out some parts for julia, as well as having access to things like DiffEq. But ray is pretty integral at this point, so I need something that will play nice with it.

@msundvick
Copy link
Author

msundvick commented Jul 22, 2021

Also, new issue really, but where's juliacall_pipdir gone? I'm getting UndefVarError when following the juliacall setup instructions while on master. Manually hunting down the location and runnning pip install is fine though. Is this just planning ahead for a release on PyPI?

@cjdoris
Copy link
Collaborator

cjdoris commented Jul 22, 2021

No I just forgot! But it will go in PyPI at some point.

I think some of Julia's code loading happens in threads which plays havoc a bit with python's GIL.

I don't know when I can get round to fixing it, hopefully the workaround you found is ok for now?

@msundvick
Copy link
Author

Sure, it's not terribly elegant but it works well enough for now. Yeah, I can totally see the GIL being a pain, so no worries if it takes a while to fix.

@cjdoris cjdoris added the bug Something isn't working label Oct 2, 2021
@github-actions github-actions bot added the stale Issues about to be auto-closed label Sep 22, 2023
@JuliaPy JuliaPy deleted a comment from github-actions bot Sep 22, 2023
@cjdoris cjdoris removed the stale Issues about to be auto-closed label Sep 22, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants