ctrl c listener for producer

This commit is contained in:
2024-10-19 16:01:27 -07:00
parent f47512010f
commit c9bf33ab6c
3 changed files with 21 additions and 2 deletions

View File

@@ -6,7 +6,7 @@ edition = "2021"
[dependencies]
server = { path = "../../server" }
tonic = "0.12.3"
tokio = { version = "1.40.0", features = ["rt-multi-thread"] }
tokio = { version = "1.40.0", features = ["rt-multi-thread", "signal"] }
chrono = "0.4.38"
tokio-util = "0.7.12"
num-traits = "0.2.19"

View File

@@ -123,9 +123,18 @@ async fn main() -> Result<(), Box<dyn Error>> {
let sin_tlm_handle = tlm.register("simple_producer/sin".into(), TelemetryDataType::Float32).await?;
let cos_tlm_handle = tlm.register("simple_producer/cos".into(), TelemetryDataType::Float64).await?;
let cancellation_token = CancellationToken::new();
{
let cancellation_token = cancellation_token.clone();
tokio::spawn(async move {
let _ = tokio::signal::ctrl_c().await;
cancellation_token.cancel();
});
}
let mut next_time = Instant::now();
let mut index = 0;
for _ in 0..100 {
while !cancellation_token.is_cancelled() {
next_time += Duration::from_millis(100);
index += 10;
tokio::time::sleep_until(next_time).await;