a strange situation about parallel requests #14642
Replies: 2 comments 17 replies
-
|
I’m not sure if I fully understood what you’re trying to accomplish, but I’ll try to help. You cannot predict the order of the responses. Even if you send the requests in a specific order, it is never guaranteed that the server will respond in the same order. In a real-world scenario, with multiple workers, servers, or pods, this becomes even harder to predict. In your example, you are using a sync function, which means your code is executed in a separate thread pool so it doesn’t block the event loop. This thread pool is managed by anyio, but the actual scheduling and execution time are handled by the operating system. You could use some kind of lock to achieve what you’re trying to do, but this approach would be limited to a single worker, server, or pod. For this use case, it might make more sense to move the processing to a worker using redis or another message broker. |
Beta Was this translation helpful? Give feedback.
-
|
Hello @walkinrain2008, i have run your mre but i m unable to reproduce your issue. |
Beta Was this translation helpful? Give feedback.


Uh oh!
There was an error while loading. Please reload this page.
-
First Check
Commit to Help
Example Code
Description
use axios to sent 2 request
server log is
it's means :
when app.get have path arg , requests is parallel processing。
when app.get no path arg, requests are being responded to one by one。
In reality, the test function is a database update feature and cannot be cached. This means that every time the test is executed, the results are different, even though the parameters have not changed.
My design is:
When the first request for test2 is received, add test to background tasks to run, and immediately return 'The task has started running.'
When test is already running and a test2 request is received, return 'The task is already running.'
But the current situation: when multiple test2 requests are received, they end up running one after another in a queue.
I have looked for official documentation but could not find relevant technical details. I am seeking help here.
Operating System
Windows
Operating System Details
windows 10
FastAPI Version
0.128.0
Pydantic Version
2.12.4
Python Version
Python 3.13.1
Additional Context
No response
Beta Was this translation helpful? Give feedback.
All reactions