Diggsey / act-zero

Apache License 2.0
122 stars 7 forks source link

Working with Tokio Runtime #10

Closed GTime closed 3 years ago

GTime commented 3 years ago

Hi, thanks for sharing such an amazing project.

I am interested in Actor and currently use actix. I am in search for a more flexible crate and I find act-zero amazing. I tried your tokio example but rust could not find tokio runtime.

   use act_zero::runtimes::tokio::spawn_actor;
   |                         ^^^^^ could not find `tokio` in `runtimes`

I am fairly new to rust, please help me out with possible solutions. Thank you

praveenperera commented 3 years ago

@GTime in your Cargo.toml you have to activate the Tokio feature for act-zero

act-zero = {version = "0.4", features = ["default-tokio"]}
GTime commented 3 years ago

@GTime in your Cargo.toml you have to activate the Tokio feature for act-zero

act-zero = {version = "0.4", features = ["default-tokio"]}

I got the following errors afterwards: code

praveenperera commented 3 years ago

You need to activate the rt or rt-multi-thread feature in Tokio to use spawn

tokio = {version = "1.2", features = ["rt-multi-thread", "time", "net"]}
GTime commented 3 years ago

You need to activate the rt or rt-multi-thread feature in Tokio to use spawn

tokio = {version = "1.2", features = ["rt-multi-thread", "time", "net"]}

I made the above changes, but still having the below errors. code

I am running the exact tokio example in the examples folder.

use act_zero::runtimes::tokio::spawn_actor;
use act_zero::*;

struct HelloWorldActor;

impl Actor for HelloWorldActor {}

impl HelloWorldActor {
    async fn say_hello(&mut self) {
        println!("Hello, world!");
    }
}

#[tokio::main]
async fn main() -> Result<(), ActorError> {
    let addr = spawn_actor(HelloWorldActor);
    call!(addr.say_hello()).await?;
    Ok(())
}
praveenperera commented 3 years ago

To use #[tokio::main] you also need the macros feature on tokio

tokio = {version = "1.2", features = ["time", "rt-multi-thread", "macros"]}
GTime commented 3 years ago

To use async main #[tokio::main] you also need the macros feature on tokio

tokio = {version = "1.2", features = ["time", "rt-multi-thread", "macros"]}

It worked, thanks very much. Forgive me for the trouble, I'm new to rust and it's ecosystem.

Please one more question: how do I run it as blocking.

praveenperera commented 3 years ago

Please one more question: how do I run it as blocking.

I'm not exactly sure what you mean, but if you want to run spawn_blocking task from tokio you need to activate the blocking feature.

https://docs.rs/tokio/1.6.1/tokio/task/fn.spawn_blocking.html

GTime commented 3 years ago

Thanks for your help.