Actix-web: Run server within #[tokio::main]

Created on 24 Nov 2019  路  5Comments  路  Source: actix/actix-web

My application currently looks like this:

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let abc = prepare_foobar().await;
    // ...
    // HttpServer::new()?
}

Is it possible to start the HTTP server within my existing runtime context?

Most helpful comment

I might have misunderstood, but looks like #[tokio::main(basic_scheduler)] also does not work.

Is there a guide or example that demonstrates how to use Tokio-based libraries from within an actix-web server?

From the actix-rt docs, it seems like you can create a system that uses Tokio under the hood. But it is not clear how to use this to start an actix-web server.

let rt = tokio::runtime::Runtime::new().unwrap();
let system = actix_rt::System::run_in_tokio("test", rt.handle());
???

Thanks.

All 5 comments

actix supports current-thread runtime, tokio starts multithreaded ru time by default

I might have misunderstood, but looks like #[tokio::main(basic_scheduler)] also does not work.

Is there a guide or example that demonstrates how to use Tokio-based libraries from within an actix-web server?

From the actix-rt docs, it seems like you can create a system that uses Tokio under the hood. But it is not clear how to use this to start an actix-web server.

let rt = tokio::runtime::Runtime::new().unwrap();
let system = actix_rt::System::run_in_tokio("test", rt.handle());
???

Thanks.

@fafhrd91 the complete lack of support for tokio doesn't look good for actix-web.

@mpfaff fafhrd91 doesnt work on actix anymore.

What are you needing to do specifically.

@robjtede my apologies. I need to use actix (or some other http server) as one component of many running simultaneously in the same program. I thought I would be able to do that by starting each component as an asynchronous task.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

zhaobingss picture zhaobingss  路  4Comments

naturallymitchell picture naturallymitchell  路  4Comments

ufosky picture ufosky  路  3Comments

joshsleeper picture joshsleeper  路  3Comments

yoshrc picture yoshrc  路  5Comments