Repository navigation
JuliaCall: add nogil option to seval - #850
Open
MilesCranmerBot wants to merge 1 commit into
Open
MilesCranmerBot wants to merge 1 commit into
MilesCranmerBot wants to merge 1 commit into
Conversation
`jl.seval(code, nogil=True)` evaluates code with the GIL unlocked, so a package can load its Julia packages while another Python thread imports its Python dependencies. The new method is precompiled, unlike a closure called with `_jl_call_nogil`. Co-authored-by: Miles Cranmer <miles.cranmer@gmail.com>
Contributor
|
@cjdoris this helped a ton with startup times in PySR! |
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Why
Python packages built on JuliaCall usually load their Julia package at import time with
jl.seval("using Foo"). That load does not use Python, but it holds the GIL, so the package cannot import its Python dependencies at the same time. This addsjl.seval(code, nogil=True)so the load can overlap with Python work on another thread.What changed
ModuleValue.seval(code, *, nogil=False). Withnogil=True, the string is converted with the GIL held, then parsed and evaluated with the GIL unlocked, and the result is converted with the GIL held again.sevalin Precompile the first calls of a JuliaCall session #837.Scope
_jl_call_nogil: the code, and the__init__of every package it loads, must not use Python.sevalwithout the keyword.Tradeoffs
_jl_call_nogilcan already do this through a closure (jl.seval("code -> Base.eval(Main, Meta.parseall(code))")._jl_call_nogil(code)), but that first call compiles the argument conversion and the closure at runtime, about 0.5 s at import in PySR._jl_seval_nogilmethod:_jl_call_nogilis a method only because__call__passes keywords through to Julia, andsevalhas no such clash.Verification
test_seval_nogil: twoseval("Libc.systemsleep(1)", nogil=True)calls on two Python threads finish in about 1 s (2.3 s with the GIL held); Julia errors raiseJuliaErrorand the GIL is locked again.import pysr2.95 s → 2.08 s (median of 7, Julia 1.13, Apple M-series).