- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个 main
函数,我在其中创建了一个 Tokio 运行时并在其上运行两个 futures。
use tokio;
fn main() {
let mut runtime = tokio::runtime::Runtime::new().unwrap();
runtime.spawn(MyMegaFutureNumberOne {});
runtime.spawn(MyMegaFutureNumberTwo {});
// Some code to 'join' them after receiving an OS signal
}
如何接收 SIGTERM
,等待所有未完成的任务(NotReady
)并退出应用程序?
最佳答案
处理信号很棘手,解释如何处理所有可能的情况过于宽泛。信号的实现不是跨平台标准的,所以我的回答是特定于 Linux 的。如果你想更跨平台,使用POSIX函数sigaction
结合pause
;这将为您提供更多控制权。
tokio 的文档非常棒getting started guide在东京的信号。因此,我将尝试添加我自己的建议。
我的一般建议是让一个任务为我们处理信号,然后您在其他任务中使用一个 watch channel ,如果 watch channel 状态发生变化,该 channel 将停止。
我的第二个建议是将 biased
与等待您的 future 的 select
一起使用,这很重要,因为您通常想知道是否立即收到信号并且之前没有做其他事情。这可能是一个经常准备好的繁忙循环的问题,你永远不会得到你的信号 future 分支。请仔细阅读 documentation关于有偏见
。
use core::time::Duration;
use tokio::{
select,
signal::unix::{signal, SignalKind},
sync::watch,
time::sleep,
};
#[tokio::main]
async fn main() {
let (stop_tx, mut stop_rx) = watch::channel(());
tokio::spawn(async move {
let mut sigterm = signal(SignalKind::terminate()).unwrap();
let mut sigint = signal(SignalKind::interrupt()).unwrap();
loop {
select! {
_ = sigterm.recv() => println!("Recieve SIGTERM"),
_ = sigint.recv() => println!("Recieve SIGTERM"),
};
stop_tx.send(()).unwrap();
}
});
loop {
select! {
biased;
_ = stop_rx.changed() => break,
i = some_operation(42) => {
println!("Result is {i}");
unsafe { libc::raise(libc::SIGTERM)};
},
}
}
}
async fn some_operation(i: u64) -> u64 {
println!("Task started.");
sleep(Duration::from_millis(i)).await;
println!("Task shutting down.");
i
}
您可以根据需要克隆 channel 的接收器,这将提高处理信号的效率。
实现您想要的一种方法是使用 tokio_signal crate 来捕捉信号,像这样:(doc example)
extern crate futures;
extern crate tokio;
extern crate tokio_signal;
use futures::prelude::*;
use futures::Stream;
use std::time::{Duration, Instant};
use tokio_signal::unix::{Signal, SIGINT, SIGTERM};
fn main() -> Result<(), Box<::std::error::Error>> {
let mut runtime = tokio::runtime::Runtime::new()?;
let sigint = Signal::new(SIGINT).flatten_stream();
let sigterm = Signal::new(SIGTERM).flatten_stream();
let stream = sigint.select(sigterm);
let deadline = tokio::timer::Delay::new(Instant::now() + Duration::from_secs(5))
.map(|()| println!("5 seconds are over"))
.map_err(|e| eprintln!("Failed to wait: {}", e));
runtime.spawn(deadline);
let (item, _rest) = runtime
.block_on_all(stream.into_future())
.map_err(|_| "failed to wait for signals")?;
let item = item.ok_or("received no signal")?;
if item == SIGINT {
println!("received SIGINT");
} else {
assert_eq!(item, SIGTERM);
println!("received SIGTERM");
}
Ok(())
}
该程序将等待所有当前任务完成并捕获选定的信号。这在 Windows 上似乎不起作用,因为它会立即关闭程序。
关于asynchronous - 如何优雅地关闭 Tokio 运行时以响应 SIGTERM?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53458755/
当运行这样的代码时: use futures::executor; ... pub fn store_temporary_password(email: &str, password: &str) -
我遵循了mdns Rust文档并粘贴了示例代码,但它抛出了以下错误:。以下是我拥有的代码:。依赖关系:。我遗漏了什么?我试着在网上寻找,但没有找到如何为这个用例创建一个反应堆。
假设我想与 Tokio 同时下载两个网页... 要么我可以用 tokio::spawn() 来实现这个: async fn v1() { let t1 = tokio::spawn(reqwe
我制作了一个还能显示天气的 LED 时钟。我的程序在一个循环中做了几件不同的事情,每件事都有不同的间隔: 每 50 毫秒更新一次 LED, 每 1 秒检查一次光照水平(以调整亮度), 每 10 分钟获
我制作了一个还能显示天气的 LED 时钟。我的程序在一个循环中做了几件不同的事情,每件事都有不同的间隔: 每 50 毫秒更新一次 LED, 每 1 秒检查一次光照水平(以调整亮度), 每 10 分钟获
tokio::run_async + futures 0.3 + tokio::net::UnixStream panic 。 设置 [package] name = "prac" version =
在我的 rust 项目中,cargo 提示使用 tokio::sync 时使用的类型不在范围内: use tokio::sync::RwLock; | ^^^^^ use of undec
我将如何使用自定义 tokio 运行时构建器并且没有主宏来实现这个 tokio_postgres 示例? 这工作正常,根据 tokio_postgres docs : 示例/withmacro.rs
目前我有一个主要的写成 async example for the Reqwest library . #[tokio::main] async fn main() -> Result> { 我们可以
我遵循the mdns Rust documentation并粘贴了示例代码,但它引发以下错误: thread 'main' panicked at 'there is no reactor runn
extern crate tokio; // 0.1.22 use tokio::io; use tokio::net::TcpListener; use tokio::prelude::*; use
我正在尝试使用 tokio 编写一个测试程序,该程序从网站获取文件并将流式响应写入文件。 hyper 网站显示了一个使用 while 循环并使用 .data() 的示例。方法响应主体,但我想用 .ma
我在 prod 中运行一个 rust Tokio 应用程序。在上一个版本中,我有一个错误,一些请求导致我的代码进入无限循环。 发生的事情是当进入无限循环的任务卡住时,所有其他任务继续正常工作并处理请求
下面的程序应该从多个线程定期打印,但是 tokio::time::sleep没有按我预期的那样工作: use tokio::prelude::*; //0.3.4 use tokio::runtime
我使用如下代码启动 Tokio 运行时: tokio::run(my_future); 我的 future 继续启动一堆任务以响应各种条件。 其中一项任务负责确定程序何时关闭。但是,我不知道如何让该任
我正在尝试构建一个可以管理来自 websocket 的提要但能够在多个提要之间切换的对象。 有一个 Feed 特征: trait Feed { async fn start(&mut self
我有一个设置,我的程序使用 std::thread::spawn 为 CPU 绑定(bind)计算生成多个线程。 我需要一个 GRPC 服务器来处理传入的命令并流式传输工作线程完成的输出。我正在为 G
我做计算机系统项目的第一个经历是使用 vanilla Java 构建服务器,然后在 Android 手机上构建客户端。从那时起,我发现有很多框架可以帮助管理可伸缩性并消除编写样板代码的需要。 我正在尝
我将从 Delphi XE4 迁移到 10.2。新的单位名称样式(深灰色背景上的黑色文本)不适合我的视力。有人可以建议如何更改它,最好不使用第 3 方加载项吗? 这就是新样式的样子,我很难阅读事件单位
我一直在寻找tokio源代码来获取问题的答案,并且给人以the sleep method literally puts a timer with duration的印象,但是我认为我可能误解了代码,因
我是一名优秀的程序员,十分优秀!