信号处理
rust
rust 信号处理:http://llever.com/cli-wg-zh/in-depth/signals.zh.html
| [dependencies]
async-std = "*"
futures = "*"
ctrlc = { version = "3.0", features = ["termination"] }
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57 | use async_std;
use async_std::prelude::*;
use async_std::task;
// use std::sync::atomic::{AtomicBool, Ordering};
// use std::sync::Arc;
use futures::{
StreamExt, SinkExt, future::FutureExt,
channel::mpsc::{unbounded, UnboundedSender, UnboundedReceiver}
};
use std::time::Duration;
async fn run(mut rx: UnboundedReceiver<bool>) {
// let wait_running = async {
// while running.load(Ordering::SeqCst) {
// println!("{}", running.load(Ordering::SeqCst));
// }
// };
// let delay = async {
// loop {
// task::sleep(Duration::from_secs(2)).await;
// }
// };
// futures::select! {
// _ = wait_running.fuse() => (),
// _ = delay.fuse() => (),
// };
let task = async {
while let Some(val) = rx.next().await {
println!("val = {}", val);
if !val {
break;
}
}
};
futures::select! {
_ = task.fuse() => (),
};
}
fn main() {
// let running = Arc::new(AtomicBool::new(true));
// let r = running.clone();
let (tx, rx) = unbounded();
ctrlc::set_handler(move || {
println!("ctrl + c");
tx.unbounded_send(false).ok();
// r.store(false, Ordering::SeqCst);
})
.expect("Error setting Ctrl-C handler");
// println!("Waiting for Ctrl-C...");
// while running.load(Ordering::SeqCst) {
// println!("{}", running.load(Ordering::SeqCst));
// }
// println!("Got it! Exiting...");
task::block_on(run(rx));
}
|
python