In one of my actix-web handlers I want to call a function that run in the background and immediately return a response to the user:
async fn heavy_computation() -> {
// do some long running computation
}
async fn index(req: HttpRequest) -> impl Responder {
// start computation
heavy_computation();
// render and return template
let out context = Context::new();
context.insert("foo", "bar");
render_template("my_template.html", &context)
// computation finishes
}
#[actix_rt::main]
async fn main() -> std::io::Result<()> {
HttpServer::new(|| {
App::new()
.service(web::resource("/").route(web::get().to(index)))
})
.bind("127.0.0.1:8080")?
.run()
.await
}
If I await
the future the response isn't done until after the computation and if I don't await
it, the function isn't executed at all.