Skip to content

Commit

Permalink
tokio instead
Browse files Browse the repository at this point in the history
  • Loading branch information
codekansas committed Sep 21, 2024
1 parent 77e6101 commit d7eb22a
Showing 1 changed file with 14 additions and 13 deletions.
27 changes: 14 additions & 13 deletions actuator/rust/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use pyo3::prelude::*;
use pyo3_stub_gen::{define_stub_info_gatherer, derive::gen_stub_pyclass};
use std::sync::{Arc, Mutex};
use std::sync::Arc;
use tokio::runtime::Runtime;
use tokio::time::{interval, Duration};
use tokio::sync::Mutex;
use std::sync::atomic::{AtomicBool, Ordering};

#[pyclass]
Expand All @@ -22,24 +23,24 @@ impl Actuator {

let runtime_clone = Arc::clone(&runtime);
let running_clone = Arc::clone(&running);
std::thread::spawn(move || {
if let Some(rt) = runtime_clone.lock().unwrap().as_ref() {
rt.block_on(async {
let mut interval = interval(Duration::from_secs(1));
while running_clone.load(Ordering::Relaxed) {
interval.tick().await;
println!("Hello, world!");
}
});
}
});

// Use the runtime to spawn the background task
if let Some(rt) = runtime_clone.try_lock().unwrap().as_ref() {
rt.spawn(async move {
let mut interval = interval(Duration::from_secs(1));
while running_clone.load(Ordering::Relaxed) {
interval.tick().await;
println!("Hello, world!");
}
});
}

Ok(Actuator { runtime, running })
}

fn stop(&mut self) -> PyResult<()> {
self.running.store(false, Ordering::Relaxed);
if let Some(runtime) = self.runtime.lock().unwrap().take() {
if let Some(runtime) = self.runtime.try_lock().unwrap().take() {
runtime.shutdown_background();
}
Ok(())
Expand Down

0 comments on commit d7eb22a

Please sign in to comment.