6个版本
| 0.0.6 | 2021年3月5日 |
|---|---|
| 0.0.5 | 2021年3月5日 |
| 0.0.2 | 2021年1月30日 |
#1524 in 异步
13KB
299 行
拖拉机
基于Pony的actor构建的Rust actor框架
-
Actor不能死锁! -
Send向Actor发送消息永远不会失败。 -
Actor中没有async。行为不能阻塞。 -
Actor会被垃圾回收。 -
与Rust中其他Actor实现相比,
tractor的实现相当简单。 -
它使用了
tokio,但之前使用的是async_std。
示例
在Cargo.toml中添加tractor = "*"和tokio = "1.2.0"。
use tractor::prelude::*;
pub struct Adder {
sum: usize,
}
#[actor(derive_hooks)]
impl Adder {
fn inc(&mut self) {
self.sum += 1;
}
fn add(&mut self, num: usize) {
self.sum += num;
}
}
fn actor_main() {
let adder: Addr<Adder> = Adder { sum: 0 }.start();
/// This sends the `inc` message to the actor.
adder.inc();
/// This sends the `add` message to the actor.
adder.add(42);
}
fn main() {
ActorSystem::run_to_completion(actor_main);
}
更多详情
-
Actor有无限大小的邮箱,并且send是非阻塞的。 -
Actor不能手动停止。一旦没有对它们的进一步引用并且它们的邮箱为空,它们就会终止。这意味着向Actor发送消息永远不会失败,除非内存耗尽。为了避免Actor过载,您可以检查其邮箱的当前长度。 -
Actor的行为没有返回值!因此,Actor不支持“等待”结果。要“模拟”请求/响应,将Actor的地址放入消息中,并对其进行响应。 -
Actor的行为不是async fn!Async意味着执行可以“暂停”。请使用异步actor(ActorBehaviorAsync/ActorHooksAsync)代替。 -
注意:任何
Actor循环都会破坏Actor的垃圾回收。
依赖关系
~2.9–9.5MB
~81K SLoC