Comments (3)
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.
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.
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)
- Implement `Clone` for `WeakRecipient`
- Panic in Arbiter thread HOT 1
- Fair handling of async sub-tasks in ContextFut
- Implement PartialEq and Eq for WeakAddr
- unable to retrieve actor addresses from registry outside of actor context HOT 1
- Question: How to wait for an event on actor startup? HOT 1
- Add discord link instead of gitter HOT 1
- "there is no reactor running" panic when system is quickly dropped HOT 2
- Actor stops receiving messages after filling the mailbox HOT 4
- Payload reached size limit. HOT 1
- Conditional inclusion of tokio-net and tokio-signal for targets that do not support them. HOT 3
- "PAGE NOT FOUND" when open the book
- Readme User Guide link is broken HOT 1
- AsyncContext::wait() is not actually waiting HOT 2
- Consider updating `actix-derive` to use `syn@2` HOT 2
- Docs: Broken links in README.md (minor issue)
- Hey,all,I have one question. HOT 2
- Question: Awaiting actor responses in message handler HOT 1
- Build fail due to "ahash v0.7.7" error[E0635]: unknown feature stdsimd HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from actix.