Will reqwest::blocking block my main-thread in this case?

Hello

I have an Actix-web project.

#[actix_web::main]
async fn main() -> std::io::Result<()> {

    // Cronjob
    let mut cron = CronJob::new("Test", cronjob_function);
    cron.seconds("20"); // Executed every minute in this example
    cron.offset(0);
    CronJob::start_job_threaded(cron); // Threaded

    // start http server
    HttpServer::new(move || {
        // Code and services for Actix-web
        //...
       .service(
                web::resource("/test")
                    .route(web::post().to(test)),
            )
       //...
    })
    .bind("127.0.0.1:8080")?
    .run()
    .await
}

I have a test-function which gets executed when the route /test gets a POST-request:

pub async fn test(bytes: Bytes, req: HttpRequest) -> Result<HttpResponse> {
    //...
}

This is my cronjob-function:

pub fn cronjob_function(name: &str) {
    let client = reqwest::blocking::Client::new();
    let res = client
        .post("http://127.0.0.1:8080/test")
        .send();
}

Basically cronjob_function gets executed every minute. It sends POST-request to /test resulting in the function test() being executed.

Note that in cronjob_function I am using reqwest::blocking which is a synchronous version of reqwest meaning that the thread it runs on stops until the POST-request is finished.

The question I have is if with this approach I will block my Actix webserver? Normally cronjob_function should be executed on a separate thread because I am using CronJob::start_job_threaded(cron);. Does that mean that only the thread for the cronjob gets 'blocked' by the request and not my main-thread where Actix runs on?

Yes.

No.

2 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.