-
Notifications
You must be signed in to change notification settings - Fork 819
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 a Python::allow_thread_unsafe
variant without a Send
bound
#2140
Comments
How about having an opt-out mechanism for individual variables? We could have My reason for suggesting this alternative is that it's a very far-reaching |
@davidhewitt Thank you for your feedback!
This sounds a bit scary to me. What if someone calls |
That's a fair point, that's not ideal either. Scrap my suggestion. It's possible to implement |
Sure! If you want I can write something and do a pull request. Should I add an example inside of the examples directory, add something the the FAQ or do something else? |
I think the FAQ is a great place to start 👍 |
I don't like the idea of |
@Tpt do you have a concrete use case where you ran into this problem? Note that if you have a use pyo3::prelude::*;
use std::rc::Rc;
fn main() {
Python::with_gil(|py| {
let shared: Rc<[u8]> = vec![0; 25].into();
let buf: &[u8] = &*shared;
py.allow_threads(|| {
println!("{:?}", buf);
});
});
} Unfortunately this won't work if you have a structure using |
Yes, this is exactly my usecase. A structure with a lot of internal |
SPARQL query is currently blocked by PyO3/pyo3#2140
@davidhewitt what would you think about just making a nightly-only feature that changes allow_threads to use our own auto trait? I think it's the best solution to this and the send_wrapper issue. |
This is now implemented as part of the https://docs.rs/pyo3/latest/pyo3/marker/index.html#a-proper-implementation-using-an-auto-trait |
The python
Python::allow_thread
method allows to release the GIL when evaluating Rust code inside of a function exposed to the Python interpreter.However, it requires the
Send
bound on the closure given for argument to it in order to make sure no object holding the GIL is an argument of this closure.This creates a strong limitation on some code that is not thread safe (e.g. use
std::rc::Rc
) but "GIL safe" because it does not hold the GIL.What about creating an unsafe version of the
Python::allow_thread
that would not enforce theSend
bound and would be annotated asunsafe
because it would require the caller to ensure the GIL is not leaked into the closure?I (@Tpt) am willing to write an implementation PR if you think this addition is relevant to pyo3.
The text was updated successfully, but these errors were encountered: