Actix-web AND CPU bound tasks

Hi guys,

I'm feeling a little bit like a small fish in a really big ocean, and would appreciate some hand holding.

Actix-web is currently working great where we reach an endpoint, we do something, we .await it and then return the result.

The problem is this: Sometimes a user may reach an end-point that requires us do some CPU heavy work that can take a long time. We don't want to .await here. We want to pass it off somewhere and be able to immediately return, perhaps with a job-id so that the client can now poll a different endpoint to check on how far it is until completion. I've looked around and have found crates like background_jobs which need to live inside of a semaphore (what the...?) or actix_web::rt::task::spawn_blocking which from the description sounds like what i need, but how do integrate that into actix-web? also futures_cpupool but the same problem as the one before.

Again, I'm kind of lost here. Can anyone guide me? This must be a very common pattern

There are some examples of offloading things using the spawn_blocking method in this blog post. The method in actix-web is a re-export of the one used in the article.

1 Like

Alice, you are incredible. Will read up. thank you.

I just finished Luca Palmieri's fantastic Zero2Production in Rust, and it demonstrates how to use spawn_blocking() to accomplish this. I think it's pretty much the same as Alice's example, though.

1 Like

Oh snap! I have that book! will look into it

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.