Skip to content
Discussion options

You must be logged in to vote

You have the same re-entrancy problem as in https://pyo3.rs/main/class/call.html#what-is-the-cell-for

Consider changing it to something like this:

#[pyclass]
pub struct Test {
-    progress: usize
+    progress: AtomicUsize
}

#[pymethods]
impl Test {
    #[new]
    pub fn new() -> Self {
-        Self {progress: 0}
+        Self {progress: AtomicUsize::new(0)}
}

-    pub fn start(&mut self, py: Python) {
+    pub fn start(&self, py: Python) {
        py.allow_threads(move || {

            for _ in 0..20 {
                std::thread::sleep(std::time::Duration::from_secs_f64(0.1));
-                self.progress += 1;
+                self.progress.fetch_add(1, Ordering::SeqCst);

Replies: 1 comment 10 replies

Comment options

You must be logged in to vote
10 replies
@songololo
Comment options

@birkenfeld
Comment options

@songololo
Comment options

@adamreichold
Comment options

@songololo
Comment options

Answer selected by KCrux
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
5 participants