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

Add Python::with_gil #1037

Merged
merged 1 commit into from
Jul 15, 2020
Merged

Add Python::with_gil #1037

merged 1 commit into from
Jul 15, 2020

Conversation

davidhewitt
Copy link
Member

As suggested in #1019, this is a new Python::with_gil API for executing a closure with the GIL.

It avoids some of the potential panics introduced in #1036, and also might be "nicer" because the usual acquire_gil(); gil.python() dance can be replaced by a single with_gil call.

I've updated some key examples in the documentation to use with_gil, as I think it's probably better to encourage users to use this new API rather than acquire_gil().

Copy link
Member

@kngwyu kngwyu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you, but I don't think the example needs to acquire GIL.

# }
# }

#[pymethods]
impl UserModel {
pub fn set_variables(&mut self, var: Vec<f64>) -> PyResult<()> {
pub fn set_variables(&mut self, var: Vec<f64>) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method should take py: Python.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, but it's actually quite hard to solve.

The trait signature impl Model for UserModel has no way to pass a Python into the trait methods. The idea for this example is to show how to wrap a Rust trait which doesn't know anything about Python, so I don't think changing the trait definition is a solution either.

For now I would like to leave it as it is, but if we come up with a good idea how to clean this up later I would be happy to resolve it. (I'll create an issue so we remember this.)

src/python.rs Show resolved Hide resolved
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 this pull request may close these issues.

2 participants