long tasks inside actix_rt::spawn blocking main thread #3052
-
I am using actix web with this crate https://github.com/rusty-celery/rusty-celery for celery background tasks and scheduling I though that sending the tasks to separate thread would not block the main thread unless am doing something wrong from within my main function am creating celery task consumer as #[actix_web::main]
async fn main() -> std::io::Result<()> {
//
// ... app state and other things
//
let app_celery = celery::app!(
broker = RedisBroker { std::env::var("REDIS_URL").unwrap() },
tasks = [
some_task // this is a task added to consumer
],
task_routes = [
"*" => QUEUE_NAME,
],
)
.await
.unwrap();
let app_celery_clone: Arc<Mutex<Arc<Celery>>> = Arc::new(Mutex::new(app_celery.clone()));
actix_rt::spawn({
let app_celery_clone = app_celery_clone.clone();
async move {
let my_app = app_celery_clone.lock().await;
my_app.consume().await.unwrap();
}
});
actix_rt::spawn({
async move {
beat_runner(QUEUE_NAME).await;
}
});
HttpServer::new(move || {
App::new()
//... more config
}).bind(server_addr)?
.run()
.await
} and my beat consumer as pub async fn beat_runner(queue_name: &str) {
let mut beat = celery::beat!(
broker = RedisBroker { std::env::var("REDIS_URL").unwrap() },
tasks = [
"renew_token" => {
get_new_token,
schedule = DeltaSchedule::new(Duration::from_secs(60 * 60)),
args = ()
},
],
task_routes = [
"*" => queue_name,
],
)
.await
.unwrap();
beat.start().await.unwrap();
} #[celery::task]
pub async fn some_task(arg_1:i32) -> TaskResult<()> {
std::thread::sleep(std::time::Duration::from_secs(60));
Ok(())
} I am then sending a task from one of my endpoint as app_state.celery_tasks
.send_task(some_task::new(arg_1))
.await?; the task is sent and is being consumed but there is some issue... for the 60 seconds that the task is running there is no http request that can be processes by the application I though that could there be something am doing wrong or that the |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Using
Not so, if you need to do signifcant blocking calls then |
Beta Was this translation helpful? Give feedback.
Using
thread::sleep
in async code is always wrong. Useactix_rt::time::sleep
instead.Not so, if you need to do signifcant blocking calls then
actix_web::web::block
is required.