Efficiently Managing CPU-Intensive Tasks in Sanic to Prevent Worker Blockage and Ensure Request Handling

I have a Sanic application running in multi-worker mode, with several APIs performing CPU-intensive tasks that can take 1-2 seconds to process. These tasks are blocking workers, preventing them from accepting new requests. How can I run these CPU-bound tasks in the background to keep my workers unblocked?

Hi!
If you are in a multi-worker mode already, and when your API requires to send a reply after CPU-intense computation ends, then your client anyway needs to keep (and occupy) connection. Multi worker mode should already sppread across available CPUs on your build host.

Most likely you may increase backlog setting (default is 100 parallel connections), if number of parallel connections is saturating out.

In a single-process mode, try making handler to run tasks in a concurrent.futures.ProcessPoolExecutor(). And most likely backlog setting increase will be needed as well.