-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
how to run server by #[tokio::main] #1283
Comments
You can not do this directly. You have to create actix_rt::System |
I do not want add actix_rt |
It is integral part of the project. |
While I don't know what will happen with this project, though I do hope it continues in some fashion in the open source world, there is a way to run actix-web directly in a Tokio runtime. It does still require actix-rt, and it's not that straight forward to setup. You can't use But you can do the following:
|
This is how it should look. But actix-web requires special initialization inside tokio. See actix/actix-web#1283.
I think the question is answered and there is not much space to further the conversation. If there is a need to continue, feel free to do so and ask to reopen. Feature requests in the spirit of the conversation should be done in a separate issue. Closed. |
@geauxvirtual Thx for your reply. That really helped me a lot. Just one minor addition. Maybe sth. changed in between but using async fn hello_world() -> impl Responder {
"Hello World!"
}
#[tokio::main]
async fn main() -> std::io::Result<()> {
let local = tokio::task::LocalSet::new();
let sys = actix_rt::System::run_in_tokio("server", &local);
let server_res = HttpServer::new(|| App::new().route("/", web::get().to(hello_world)))
.bind("0.0.0.0:8000")?
.run()
.await?;
sys.await?;
Ok(server_res)
} |
I am struggling to get this So far it is not clear how to arrange this. Basic requirements are this:
Appreciate any thoughts on how to accomplish this. I do not understand why this was implemented using a |
There is no point of doing that. actix-web would spin up it's own threads regardless. You only start actix-web in a tokio runtime. |
Ok thanks. I assumed that the AsyncSystemRunner would utilize the existing Tokio runtime threads by running Arbiters on each one as necessary. No idea where I got that assumption; maybe it was just hope :) If the System is going to start threads regardless, I don't see the point of having the Thanks for your input. |
The reason is actix-web uses !Send futures. So it's impossible to spawn any futures in a threaded runtime. |
So to be clear:
Is that accurate? I care more about inheriting an existing Tokio runtime than I do about "extra threads." If you're saying that actix-web is going to run its own Tokio runtime anyways, then I'll just use |
actix-web would spawn a thread for listening sockets and distribute incoming streams to workers that all runs in it's own tokio current-thread runtime. |
To work off of @svenallers great post, one can use async fn hello_world() -impl Responder {
"Hello World!"
}
#[tokio::main]
async fn main() -std::io::Result<(){
let local = tokio::task::LocalSet::new();
let sys = actix_web::rt::System::run_in_tokio("server", &local);
let server_res = HttpServer::new(|| App::new().route("/", web::get().to(hello_world)))
.bind("0.0.0.0:8000")?
.run()
.await?;
sys.await?;
Ok(server_res)
} |
Hi @fakeshadow and @jnicholls Just to understand correctly as I'm in the same situation (wanting to run
|
what is |
@chaoky That's a good question. I'm not sure. At first glance, it looks like it's there to give |
I wonder if anyone could give me a hand with this issue. I am using tokio, actix and actix-web. I got "thread 'main' panicked at 'spawn_local called from outside of a task::LocalSet'" :(. Does anyone have a clue about how to resolve this? |
Possibly a mis-match of Tokio versions; current stable actix-web (v3.3) supports Tokio v0.2. Tokio v1 support coming soon. |
I've the same problem than @isubasinghe. Running
|
Need to see code to help further. |
a and in general don't tokio::main on actix-web when you can. actix-web has it's main macro for a very good reason. |
Yup its not related to tokio version. I used the same version used in the library itself. |
Could you kindly share how you did this? I'm trying to run actix_web in a tokio runtime because I have a lot of essential services which use tokio runtime (e.g. lapin for AMQP and Async redis). It's hassle wrapping their functions around a crudely-made tokio runtime, or bouncing a tokio runtime in the HttpServer as part of the |
@Overdash You can spin up a new thread with |
@jnicholls I see - thanks. Stupid question: how would I run the |
@Overdash No problem. There are no stupid questions! :) Procedural macros like The
into this
|
@jnicholls I have seen that with new |
@thangchung That method is unstable for the time being (it's doc hidden). If that's okay with you, check out this example: https://github.com/actix/actix-net/blob/master/actix-rt/examples/hyper.rs. |
This PR would hopfully finally close this. |
@fakeshadow any news :)? |
@iuslkae I have implemented a bit to work around at https://github.com/thangchung/actix-dapr-rs/blob/main/dapr-subscriber/src/main.rs#L106 |
Try out actix-web 4.0.0 beta channel. I use actix-web 4.0.0-beta.5 and can use tokio 1.x runtime without setting up an additional independent runtime |
@Overdash can you post an example 🥺 |
@iuslkae pretty straight forward, use |
I do the following. This creates 8
Cargo.toml ->
|
I want to use |
Any update on this? |
Actix Web v4 works with |
Actix-web 4.0 has direct support for |
Link to docs for an example - https://docs.rs/actix-web/latest/actix_web/rt/index.html#running-actix-web-using-tokiomain |
use actix_web::{middleware, web, App, HttpRequest, HttpServer};
async fn index(_req: HttpRequest) -> &'static str {
"Hello world!"
}
#[tokio::main]
#[cfg(unix)]
async fn main() -> std::io::Result<()> {
::std::env::set_var("RUST_LOG", "actix_server=info,actix_web=info");
env_logger::init();
}
#[cfg(not(unix))]
fn main() -> std::io::Result<()> {
println!("not supported");
Ok(())
}
The text was updated successfully, but these errors were encountered: