27个版本
0.6.2 | 2024年6月21日 |
---|---|
0.6.1 | 2024年2月14日 |
0.5.1 | 2024年2月12日 |
0.4.11 | 2023年10月6日 |
0.2.5 | 2021年9月18日 |
#310 在 异步
每月120次下载
98KB
1K SLoC
启示录:Rust的简单actor框架。
正在开发中,当前状态下不可用
Acopacypse基于我在actix框架上的经验,但完全异步地直接构建在tokio上,并且也非常简化。
以下是一个使用此框架的简单示例
use apocalypse::{Hell, Demon};
// Human demon that echoes a message with its name
struct Human {
name: String
}
// Demon implementation for the human
impl Demon for Human {
type Input = String;
type Output = String;
async fn handle(&mut self, message: Self::Input) -> Self::Output {
format!("Hey, {} here: {}", self.name, &message)
}
}
#[tokio::main]
async fn main() {
// We create one demon
let carlos = Human{
name: "Carlos".to_string()
};
// We create a hell for this
let hell = Hell::new();
let join_handle = {
let (gate, jh) = match hell.ignite().await {
Ok(v) => v,
Err(e) => panic!("Could not light up hell, {}", e)
};
// We spawn the demon in the running hell through the gate
let location = match gate.spawn(carlos).await {
Ok(v) => v,
Err(e) => panic!("Could not spawn the demon, {}", e)
};
tokio::spawn(async move {
let m1 = gate.send(&location, "hello world".to_string()).await.unwrap();
// We print the message just for show
println!("{}", &m1);
// And check that it is correct
assert_eq!("Hey, Carlos here: hello world", &m1);
});
jh
};
// We wait for all messages to be processed.
join_handle.await.unwrap();
}
死锁
目前,每个demon都有自己的线程来处理请求,因此唯一可能导致死锁的方式是创建一个从demon开始并结束在同一个demon上的请求环。不惜一切代价避免这种情况。
依赖关系
~4–15MB
~200K SLoC