gpt4 book ai didi

multithreading - Actix 系统,多个仲裁器 = 多少个线程

转载 作者:行者123 更新时间:2023-12-05 04:46:20 29 4
gpt4 key购买 nike

问题 - 如果您只有一个系统但其中有多个仲裁器在运行,它仍然是一个单线程事件循环吗?

通读 Actix 书 - https://actix.rs/book/actix/sec-6-sync-arbiter.html

When you normally run Actors, there are multiple Actors running on the System's Arbiter thread, using its event loop

还有来自 https://actix.rs/book/actix/sec-5-arbiter.html

While it only uses one thread, it uses the very efficient event loop pattern which works well for asynchronous events. To handle synchronous, CPU-bound tasks, it's better to avoid blocking the event loop and instead offload the computation to other threads. For this usecase, read the next section and consider using SyncArbiter.

重复问题 - 如果您有一个系统但有多个仲裁器在运行,它仍然是一个单线程事件循环吗?

例子

let sys = System::new();
sys.block_on( async move {
let arbiter1 = Arbiter::new(); // one arbiter here
arbiter1.spawn( SOME ACTOR);
let arbiter2 = Arbiter::new(); // another arbiter here
arbiter2.spanw( SOME OTHER ACTOR);
});

sys.run().unwrap();

这是否在单线程中运行?

当我使用 log4rs 记录它时,我看到以下内容

[actix-rt|system:0|arbiter:0]
[actix-rt|system:0|arbiter:1]

鉴于它是 system:0 - 这是否意味着它是同一个线程,只是使用了不同的仲裁器?

我是否需要运行多个 System::new 才能在 actix 中实现适当的多线程?

最佳答案

引用完全相同的book :

To use Actix in a concurrent way, you can spin up multiple Arbiters using Arbiter::new, ArbiterBuilder, or Arbiter::start.

同样在 Arbiter::new() 的文档中

Spawn a new Arbiter thread and start its event loop.

所以你绝对可以有一个多线程的事件循环。

下面是一个例子。在执行过程中,使用进程监控软件查看线程数(例如 MacOS 的 Activity Monitor、Windows 的 Process Explorer、Linux 的 htop)。

extern crate actix;
use actix::prelude::*;

struct Fibonacci(pub u32);


struct SomeActor;

impl Actor for SomeActor {
type Context = Context<Self>;
}

impl Message for Fibonacci{
type Result = Result<u64, ()>;
}

impl Handler<Fibonacci> for SomeActor {
type Result = Result<u64, ()>;

fn handle(&mut self, msg: Fibonacci, context: &mut Self::Context) -> Self::Result {
println!("working on fib({})", msg.0);
let mut sum=0;
if msg.0 == 0 {
Err(())
} else if msg.0 == 1 {
Ok(1)
} else {

for j in 1..1000000 {
let mut i = 0;
sum = 0;
let mut last = 0;
let mut curr = 1;
while i < msg.0 - 1 {
sum = last + curr;
last = curr;
curr = sum;
i += 1;
}
}
Ok(sum)
}
}
}

fn main() {
let sys = System::new();

let a1 = Arbiter::new();
let a2 = Arbiter::new();
let a3 = Arbiter::new();

let execution1 = async {
println!("exec 1 created");
let sa = SomeActor {}.start();
for n in 2..80 {
sa.do_send(Fibonacci(n));
}
};

let execution2 = async {
println!("exec 2 created");
let sa = SomeActor {}.start();
for n in 2..80 {
sa.do_send(Fibonacci(n));
}
};

let execution3 = async {
println!("exec 3 created");
let sa = SomeActor {}.start();
for n in 2..80 {
sa.do_send(Fibonacci(n));
}
};


a1.spawn(execution1);
a2.spawn(execution2);
a3.spawn(execution3);

sys.run();
}

关于multithreading - Actix 系统,多个仲裁器 = 多少个线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68869371/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com