gpt4 book ai didi

rust - 如何从actix SyncContext向另一个参与者发送消息?

转载 作者:行者123 更新时间:2023-12-03 11:36:41 26 4
gpt4 key购买 nike

我想实现一个长期运行的后台任务,该任务可以将进度报告给其他Actor。我已经做到了。
但我也希望能够再次取消长期运行的后台任务。
到目前为止,我得到的是:

use actix::prelude::*;

struct Worker {}

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

struct Manager {
worker: Addr<Worker>,
}

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

impl Supervised for Manager {}

impl SystemService for Manager {
fn service_started(&mut self, _ctx: &mut Context<Self>) {}
}

struct Work {}

#[derive(Message)]
#[rtype(result = "()")]
struct PerformWork(Work);

#[derive(Message)]
#[rtype(result = "()")]
pub struct ReportProgress(i32);

impl Handler<PerformWork> for Worker {
type Result = ();

fn handle(&mut self, msg: PerformWork, ctx: &mut Self::Context) -> Self::Result {
for i in 0..10000000 {
// Report progress
Manager::from_registry().do_send(ReportProgress(i));
// Do some very slow I/O.
thread::sleep(time::Duration::from_millis(1));
}
}
}

impl Handler<ReportProgress> for Manager {
type Result = ();

fn handle(&mut self, msg: ReportProgress, ctx: &mut Self::Context) -> Self::Result {
// Do something with the progress here
}
}
Manager还处理将 Message PerformWork发送到 MessageWorker
我想到了给 ReportProgress Message一个 bool返回类型,该类型允许 Worker决定是否应该退出其循环。但是,我无法将带有返回结果的 Message发送到 Manager
使用 send()而不是 do_send()会返回我无法在 Future中解析的 SyncContext
任何想法都非常感谢。
更多背景:
  • 真正缓慢的I/O是串行通信。
  • actix是版本0.10
  • 最佳答案

    我找到了解决方案,但我不认为这是一个好的解决方案。
    我添加了一个Arc<AtomicBool>>,将其传递给WorkerManager保留对AtomicBool的引用,并可以对其进行修改。如果WorkerAtomicBool修改,则Manager会跳出循环。

    use actix::prelude::*;
    use std::sync::atomic::{AtomicBool, Ordering};

    struct Worker {}

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

    struct Manager {
    worker: Addr<Worker>,
    }

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

    impl Supervised for Manager {}

    impl SystemService for Manager {
    fn service_started(&mut self, _ctx: &mut Context<Self>) {}
    }

    struct Work {}

    #[derive(Message)]
    #[rtype(result = "()")]
    struct PerformWork(Work, Arc<AtomicBool>>);

    #[derive(Message)]
    #[rtype(result = "()")]
    pub struct ReportProgress(i32);

    impl Handler<PerformWork> for Worker {
    type Result = ();

    fn handle(&mut self, msg: PerformWork, ctx: &mut Self::Context) -> Self::Result {
    for i in 0..10000000 {
    // Report progress
    Manager::from_registry().do_send(ReportProgress(i));
    if msg.1.load(Ordering::Relaxed) {
    break;
    }
    // Do some very slow I/O.
    thread::sleep(time::Duration::from_millis(1));
    }
    }
    }

    impl Handler<ReportProgress> for Manager {
    type Result = ();

    fn handle(&mut self, msg: ReportProgress, ctx: &mut Self::Context) -> Self::Result {
    // Do something with the progress here
    }
    }

    关于rust - 如何从actix SyncContext向另一个参与者发送消息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64786513/

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