Simple synchronous route seems to block app
See original GitHub issueFirst Check
- I added a very descriptive title to this issue.
- I used the GitHub search to find a similar issue and didn’t find it.
- I searched the FastAPI documentation, with the integrated search.
- I already searched in Google “How to X in FastAPI” and didn’t find any information.
- I already read and followed all the tutorial in the docs and didn’t find an answer.
- I already checked if it is not related to FastAPI but to Pydantic.
- I already checked if it is not related to FastAPI but to Swagger UI.
- I already checked if it is not related to FastAPI but to ReDoc.
Commit to Help
- I commit to help with one of those options 👆
Example Code
from fastapi import FastAPI, Request, Depends, HTTPException, status
import uvicorn
import time
app = FastAPI()
@app.get('/')
def main():
time.sleep(5)
return []
uvicorn.run(app, host="127.0.0.1", port=5001)
Description
I expect this code to be non-blocking, since it is declared using a regular def
instead of async def
, meaning each time I open the main route, it should load in a separate thread. However, when I run this application, and open up the /
route on multiple tabs in my browser, it is clear that the app is running synchronously. It takes 5 seconds for each tab to load the route, starting from the time the previous tab is loaded. So if I open 4 tabs, it takes about 20 seconds for the last tab to load.
Am I misunderstanding something? Is there a way to get code like this to run so that each time the page is loaded, it doesn’t block other pages from being loaded?
Operating System
Windows
Operating System Details
No response
FastAPI Version
0.70.1
Python Version
3.9.5
Additional Context
No response
Issue Analytics
- State:
- Created 2 years ago
- Comments:6 (1 by maintainers)
Top Results From Across the Web
Express.js Routes Response Synchronously - Stack Overflow
So i decided to create a simple app and test it. const config = { port : 8080 }; const express = require('express');...
Read more >Routing in ASP.NET Core - Microsoft Learn
Routing is responsible for matching incoming HTTP requests and dispatching those requests to the app's executable endpoints.
Read more >Node.js Event-Loop: How even quick Node.js async functions ...
Given the “single thread event loop” knowledge, it's obvious why the code in /compute-sync blocked all other requests: before it completed it ...
Read more >Synchronous or asynchronous, and why wrestle with wrappers?
Writing synchronous, blocking, procedural code seems simple. The business logic is expressed as a sequence of steps.
Read more >A Guide to Error Handling in Express.js | Scout APM Blog
Synchronous Code Here's an example of a route handler function where we simulate an error condition by throwing an error: app.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Ohhhhhh, interesting. Thanks for letting me know!
This is not related to the FastAPI, conversely it is a browser feature.
See the similar issues: https://github.com/tiangolo/fastapi/issues/2786 https://github.com/tiangolo/fastapi/issues/3697
In chromium there is a cache that prevents sending the same requests to same URL to avoid downloading the same data over and over again by adding a 20 second timeout to cache.
If you are interested in how it works check this issue in chromium.
See: https://codereview.chromium.org/345643003