Await in actix::prelude::AsyncContext::run_interval

Hey everyone! :slight_smile:

I made an actor that runs periodically. It works just fine but now I want to call an async function from inside of closure:

struct MyActor;

impl Actor for MyActor {
    type Context = Context<Self>;

    fn started(&mut self, ctx: &mut Self::Context) {
        ctx.run_interval(Duration::from_millis(100), |_this, _ctx| {
            two().await;
        });
    }
}

async fn two() -> i32 {
    2
}

How can I accomplish this?

Couldn't you spawn an async block using spawn?

1 Like

Yeah it worked. Thanks!

struct MyActor;

impl Actor for MyActor {
    type Context = Context<Self>;

    fn started(&mut self, ctx: &mut Self::Context) {
        ctx.run_interval(Duration::from_millis(100), |_this, _ctx| {
            Arbiter::spawn(async {
                two().await;
            });
        });
    }
}

async fn two() -> i32 {
    2
}
1 Like