Skip to content

How to understand Python::with_gil and Py<Myclass>? #2255

Answered by adamreichold
sun-rs asked this question in Questions
Discussion options

You must be logged in to vote

A value py: Python<'a> is a proof that the GIL is held, i.e. all functions which require you to have the GIL express this by requiring you to pass such a py: Python<'a> token as an argument. One example is Py::as_ref which produces a reference into the Python heap which is valid as long as the GIL is held, i.e. its lifetime 'a is bound to that of the token.

Python::with_gil is a function to acquire the GIL, i.e. it will check whether the GIL is already held, lock it if not and depending on that also release it before returning. In this way, it is a safe method to produce such a py: Python<'a> token.

Methods exposed as #[pymethods] are supposed to be called from Python meaning that the GIL…

Replies: 1 comment 6 replies

Comment options

You must be logged in to vote
6 replies
@adamreichold
Comment options

@sun-rs
Comment options

@adamreichold
Comment options

@sun-rs
Comment options

@adamreichold
Comment options

Answer selected by birkenfeld
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants
Converted from issue

This discussion was converted from issue #2254 on March 28, 2022 13:20.