Module actix_web::rt

source ·
Expand description

A selection of re-exports from tokio and actix-rt.

Actix Web runs on Tokio, providing full1 compatibility with its huge ecosystem of crates. Each of the server’s workers uses a single-threaded runtime. Read more about the architecture in actix-rt’s docs.

§Running Actix Web Without Macros

use actix_web::{middleware, rt, web, App, HttpRequest, HttpServer};

async fn index(req: HttpRequest) -> &'static str {
    println!("REQ: {:?}", req);
    "Hello world!\r\n"
}

fn main() -> std::io::Result<()> {
    rt::System::new().block_on(
        HttpServer::new(|| {
            App::new().service(web::resource("/").route(web::get().to(index)))
        })
        .bind(("127.0.0.1", 8080))?
        .run()
    )
}

§Running Actix Web Using #[tokio::main]

If you need to run something that uses Tokio’s work stealing functionality alongside Actix Web, you can run Actix Web under #[tokio::main]. The Server object returned from HttpServer::run can also be spawned, if preferred.

Note that actix actor support (and therefore WebSocket support through actix-web-actors) still require #[actix_web::main] since they require a System to be set up.

Also note that calls to this module’s spawn() re-export require an #[actix_web::main] runtime (or a manually configured LocalSet) since it makes calls into to the current thread’s LocalSet, which #[tokio::main] does not set up.

use actix_web::{get, middleware, rt, web, App, HttpRequest, HttpServer};

#[get("/")]
async fn index(req: HttpRequest) -> &'static str {
    println!("REQ: {:?}", req);
    "Hello world!\r\n"
}

#[tokio::main]
async fn main() -> std::io::Result<()> {
    HttpServer::new(|| {
        App::new().service(index)
    })
    .bind(("127.0.0.1", 8080))?
    .run()
    .await
}

  1. Crates that use Tokio’s block_in_place will not work with Actix Web. Fortunately, the vast majority of Tokio-based crates do not use it. 

Modules§

  • TCP/UDP/Unix bindings (mostly Tokio re-exports).
  • Asynchronous signal handling (Tokio re-exports).
  • Task management (Tokio re-exports).
  • Utilities for tracking time (Tokio re-exports).

Macros§

  • Pins a value on the stack.

Structs§

  • A Tokio-based runtime proxy.
  • A manager for a per-thread distributed async runtime.
  • Runner that keeps a System’s event loop alive until stop message is received.

Functions§

  • Spawns a future on the current thread as a new task.