Multiprocessing in async function?
My goal is to build a webservice for a calculation. while each individual row can be calculated fairly quickly, the use-case is tens of thousands or more rows per call to be calculated. So it must happen in an async function.
the actual calculation happens externally via cli calling a 3rd party tool. So the idea is to split the work over multiple subproccess calls to split the calculation over multiple cpu cores.
My question is how the async function doing this processing must look like. How can I submit multiple subprocesses in a correct async fasion (not blocking main loop)?
17 comments · 100% upvoted · other discussions
Remember that python is inherintly single threaded due to the GIL. You can mitigate this by running fastapi with multiple workers. The requests will then be spread over those different workers.
mrbubs3 5 points · May 2025
You can turn GIL off in 3.13
Asleep-Budget-9932 9 points · May 2025
That feature is experimental and should not be used in production environments.
bbrother92 1 points · May 2025
api requiest are dipenced to dif workers? not the treads?
Blakex123 3 points · May 2025
If u are using uvicorn there is an extra process made that essentially "load balances" the 4 workers. I assume it works the same way with any other server.
RationalDialog [OP] 1 points · May 2025
Blakex123 1 points · May 2025
Then u will need to spawn subprocesses from the api to handle the cpu intensive stuff.