Can we read python class attribute without gil in Rust? #2716
-
Greetings, I'd like to stop a Rust loop from python side. Say we have a python class class Flag:
stop = False
f = Flag() In Rust we have a function that will be running for a long time. I wish that once I set fn func_take_long_time(py: Python, object: &PyAny) {
let stop_flag: &bool = // can we get the reference of `f.stop` as `&bool`?
while true {
if *stop_flag { break; }
// .. do some time critical work
}
} |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 4 replies
-
No, this is not possible. Among other things, there is no However, what you may be able to do is to move setting the stop flag to Rust, by providing a method/function |
Beta Was this translation helpful? Give feedback.
No, this is not possible. Among other things, there is no
&bool
that would change, since Pythonbool
objects are immmutable. Reassigningf.stop = True
sets the "stop" entry inf.__dict__
to theTrue
object instead of theFalse
object.However, what you may be able to do is to move setting the stop flag to Rust, by providing a method/function
set_stop_flag()
implemented in Rust. Then, you can set an actual Rustbool
flag (well, probablyAtomicBool
) and check it cheaply from Rust code.