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

What does it take for pyjulia to drop GIL? #535

Open
Moelf opened this issue Jul 25, 2023 · 13 comments · May be fixed by #540
Open

What does it take for pyjulia to drop GIL? #535

Moelf opened this issue Jul 25, 2023 · 13 comments · May be fixed by #540

Comments

@Moelf
Copy link

Moelf commented Jul 25, 2023

Numba has a nogil annotation, I'm wondering if we can somehow drop the GIL and what would that imply.

@mkitti
Copy link
Member

mkitti commented Jul 26, 2023

I think we would need to reformulate the binding as a C or Rust based extension rather than using ctypes

@yuyichao
Copy link

It seems that using CDLL rather than PyDLL will release the Gil.

@mkitti
Copy link
Member

mkitti commented Jul 26, 2023

We do use CDLL for unix like systems:

libdl = ctypes.CDLL(ctypes.util.find_library("dl"))

@yuyichao
Copy link

If I understand correctly, it’s the one for libjulia that matters.

self.libjulia = ctypes.PyDLL(libjulia_path, ctypes.RTLD_GLOBAL)

@Moelf
Copy link
Author

Moelf commented Jul 26, 2023

yeah, I think because you're calling Julia via the libjulia, so whether or not GIL is dropped during call depends on how you loaded libjulia?

@Moelf
Copy link
Author

Moelf commented Jul 26, 2023

maybe it's worth getting perspective from @stevengj and @MilesCranmer ?

  1. Can we by default drop GIL (i.e. use CDLL)?
  2. Does it actually drop GIL and speed things up?
  3. Does it create corretness issues?
  4. Given 3, how can we drop GIL call by call

@MilesCranmer
Copy link
Collaborator

I would love to drop GIL. This would be great so that I can do other things in Python (like printing updates from another thread) while some large Julia processing is happening in the background.

Regarding 3, I'm not sure what correctness issues it would cause, if any. It's unclear to me if you'd be able to mutate any Julia variables while processing is already taking place, so maybe it's safe as-is...? Maybe we should just try it out and see.

@Moelf
Copy link
Author

Moelf commented Jul 27, 2023

yeah I'd like to try it, but I would prefer if we can present it as an option. In this case, it would be better if it can be a per-call site option, but a per-session option would do.

One case we know definitely won't work is if Julia needs to call Python stuff as call back, or mutate Python variable, but I feel like that's minor usage

@yuyichao
Copy link

I assume just having two libjulia handle should allow having this as a per call option with the ctypes api.

@Moelf
Copy link
Author

Moelf commented Jul 27, 2023

I can't quite figure out how to have a local testable pyjulia, clone and pip install -e . didn't quite work, or maybe I'm looking at the wrong place to print debug

@Moelf
Copy link
Author

Moelf commented Jul 27, 2023

I guess for one I can't figure out which handle is used when I do

import julia
#vs.
from julia import Base

I can't get any print statement to work -- these two must share the same Julia handle right? but which one / where I can't quite figure out

@stevengj
Copy link
Member

See also JuliaPy/PythonCall.jl#343

@Moelf
Copy link
Author

Moelf commented Aug 23, 2023

I think the main use case is when calling Julia code from Python, releasing the GIL would be useful.

@Moelf Moelf linked a pull request Nov 3, 2023 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants