file - 如何从文件更改中热重载结构
问题描述
我正在尝试在文件更改时进行热重新加载,但出现此错误:期望一个实现该Fn
特征的闭包,但此闭包仅实现FnMut
此闭包 implements FnMut
,而不是Fn
似乎对我从这个库传递给 new_immediate 函数的闭包不满意:
notify = { version = "5.0.0-pre.4", features = ["serde"] }
我的代码:
use announcer::messages::{load_config, save_config, Config, Message};
use notify::{
event::ModifyKind, Error, Event, EventFn, EventKind, RecommendedWatcher, RecursiveMode, Watcher,
};
use tide::{http, Body, Response};
const CONFIG_PATH: &str = "config.json";
#[async_std::main]
async fn main() -> tide::Result<()> {
let mut config = load_config(CONFIG_PATH).unwrap();
let mut watcher: RecommendedWatcher =
Watcher::new_immediate(|result: Result<Event, Error>| {
let event = result.unwrap();
if event.kind == EventKind::Modify(ModifyKind::Any) {
config = load_config(CONFIG_PATH).unwrap();
}
})?;
watcher.watch(CONFIG_PATH, RecursiveMode::Recursive)?;
let mut app = tide::with_state(config);
app.listen("127.0.0.1:8080").await?;
Ok(())
}
我在 Rust Discord 初学者聊天中询问,17cupsofcoffee 说我应该使用互斥锁,但我不知道该怎么做。
解决方案
这里的问题是您在另一个线程中生成了这个监视函数,并且您可能在一个线程中写入它,同时在另一个线程中读取它,从而导致竞争条件。您应该使用Mutex
, 和lock
它来获得一个保护,让您从中读取/写入它。由于tide
的全局状态也需要Clone
,因此您还应该将其包装在Arc
一个线程安全的引用计数指针中:
use announcer::messages::{load_config, save_config, Config, Message};
use notify::{
event::ModifyKind, Error, Event, EventFn, EventKind, RecommendedWatcher, RecursiveMode, Watcher,
};
use tide::{http, Body, Response};
// necessary imports
use std::sync::{Arc, Mutex};
const CONFIG_PATH: &str = "config.json";
#[async_std::main]
async fn main() -> tide::Result<()> {
// we store it in an Arc<Mutex<T>>
let config = Arc::new(Mutex::new(load_config(CONFIG_PATH).unwrap()));
let cloned_config = Arc::clone(&config);
let mut watcher: RecommendedWatcher =
Watcher::new_immediate(move |result: Result<Event, Error>| {
let event = result.unwrap();
if event.kind == EventKind::Modify(ModifyKind::Any) {
// we lock the mutex to acquire a mutable guard and write to that
match load_config(CONFIG_PATH) {
Ok(new_config) => *cloned_config.lock().unwrap() = new_config,
Err(error) => println!("Error reloading config: {:?}", error),
}
})?;
watcher.watch(CONFIG_PATH, RecursiveMode::Recursive)?;
let mut app = tide::with_state(config);
app.listen("127.0.0.1:8080").await?;
Ok(())
}
然后,每当您需要从状态中读取时,您只需锁定互斥锁:
let config_guard = req.state().lock().unwrap();
println!("{:?}", config_guard.foo);
更多参考:
推荐阅读
- http - 为什么 http 请求错误会停止我的应用程序?
- javascript - 无论我点击哪个选项,我都会得到相同的结果 - Rock Paper Scissors JS
- python - 拟合 sigmoid 曲线 (Python)
- java - 有没有办法在画布循环中制作图像并一遍又一遍地重绘自己?
- javascript - 上传包含空子文件夹的文件夹
- python - 在 panda.Dataframe 中有效地有条件地重新定位元素
- debugging - Redis Lua 调试器:禁用大数的科学记数法
- python - 如何获得没有元音的单词列表?
- javascript - 为什么我不能从当前状态中获取一个值并在我的 useState 挂钩中使用它?
- python - 断言类方法