我正在try 创建一个线程来监视铁 rust 中的文件更改.我正在使用notify crate 来获取文件更改事件.
下面的代码显示了监视线程中运行的内容:
use std::{path::PathBuf, thread};
use log::{error, info};
use notify::Watcher;
fn main() {
env_logger::init();
let (tx, rx) = std::sync::mpsc::channel();
thread::spawn({
let config_path = PathBuf::from("config.toml");
move || {
let mut watcher =
notify::recommended_watcher(move |res: Result<notify::Event, notify::Error>| {
match res {
Ok(event) => match event.kind {
notify::event::EventKind::Modify(notify::event::ModifyKind::Data(
notify::event::DataChange::Any,
)) => {
info!("Reloading config file");
tx.send(true).ok();
}
_ => (),
},
Err(e) => {
error!("Notify Error: {}", e);
}
}
})
.unwrap();
watcher.watch(&config_path, notify::RecursiveMode::NonRecursive)
}
});
loop {
match rx.recv() {
Ok(_) => {
// reload config
}
Err(e) => {
error!("Error while watching config file: {}", e);
break;
}
}
}
}
# Cargo.toml
[dependencies]
env_logger = "0.10.0"
log = "0.4.19"
notify = "6.0.1"
问题是,当我try 从通道接收时,我得到了输出Error while watching config file: receiving on a closed channel
.
我期望发送器tx
在不关闭通道的情况下移动到封闭器中.但不知何故,它被关闭了.
我已经判断了known problems个通知箱,但我认为没有任何与我的问题有关的.