Giter VIP home page Giter VIP logo

Comments (3)

thalesfragoso avatar thalesfragoso commented on May 27, 2024 1

wait and spawn calls don't block, they just queue the ActorFuture and return.

For your simple case, you just need to move the do_something to inside the async block:

async move {
    let result = other_actor.send(Message).await.unwrap();
    do_something(result);
}

Now, if you want access to the actor after/between await points, then you need to use combinators, see a full example below:

use actix::prelude::ContextFutureSpawner;
use actix::{Actor, ActorFutureExt, Addr, Context, Handler, Message, WrapFuture};

#[derive(Message)]
#[rtype(result = "Result<bool, ()>")]
struct Msg;

struct B;

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

impl Handler<Msg> for B {
    type Result = Result<bool, ()>;

    fn handle(&mut self, _msg: Msg, _ctx: &mut Context<Self>) -> Self::Result {
        Ok(true)
    }
}

struct A {
    b: Addr<B>,
}

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

    fn started(&mut self, ctx: &mut Context<Self>) {
        let other = self.b.clone();

        async move { other.send(Msg).await.unwrap().unwrap() }
            .into_actor(self)
            .map(|res, act, _ctx| do_something(res, act))
            // Or use .wait if you want to block the actor until the future is completed
            .spawn(ctx);
    }
}

fn do_something(_msg: bool, _actor: &mut A) {}

Furthermore what is the reasoning behind not having all the actor methods be async in order to not have the sync and async clash?

First, async traits isn't really a thing yet. Second, and most important, the async method would mutable borrow the actor for the whole future duration, which would force the actor to handle a single message (future) at a time. See the #438 discussion for more information on the subject.

from actix.

DurkelTheDonkey avatar DurkelTheDonkey commented on May 27, 2024

I see, if you can run an entire method as both async and sync like this then this does solve my issue. Thank you.

from actix.

thalesfragoso avatar thalesfragoso commented on May 27, 2024

I see, if you can run an entire method as both async and sync like this then this does solve my issue. Thank you.

The trait method is always sync, you aren't running anything async there, you're just creating a future and spawning it on the Actor's Context. spawn will just enqueue the future and return.

from actix.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.