What is the preferred way to run pollable futures on the Actix runtime? Should I implement Future for some type, and thereafter, use Arbiter::spawn()
thereon?
Consider creating an Actor
for long running async tasks in Actix. Sending messages to the Actor
will allow it to perform useful work, and it will respond asynchronously as results become available.
But yes, Arbiter::spawn()
is a good way to spawn actors on the System
.
Thank you.
Suppose I have an async closure as such
async fn long_task() -> Result<(), Box<dyn Error>> {
download_web_page().await // this will take time
}
How do I execute this from the context of an actor?
It's sort of a bummer that we can't use .await
inside the handle
, starting
, stopping
, and other functions associated with Actor's and Handlers
Yes, Actix predates async/await
support in Rust by about 2 years. The consequence of this is that the Actix ecosystem doesn't really play nicely with it. There have been some recent improvements with the release of 0.9.0...
However, it seems like I may have misunderstood what you meant by "run pollable futures" ... (All futures can be polled.) If you just want to write plain old async functions (meaning you don't necessarily need a long-lived service), you can hide all of the actor boilerplate and just use actix-rt
as an async runtime.
One example worth pointing out is async_ex1
, which uses actix-web
to provide an HTTP server, and async
function to make HTTP requests to httpbin.
But, as of now, the actor model is not compatible with async/await. Hmm, sounds like a good project that would be very fruitful.
I do need to keep a session alive and reactive to requests using websockets. The actors respond to websocket inbound packets in my setup.
You can look into some example actor implementations, like actix_redis::RedisActor
. It's all futures compositions, as you might imagine. No async functions or await keywords in sight!
(The higher-level RedisSession
internals does have this sugar, though.)
This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.