question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Simple synchronous route seems to block app

See original GitHub issue

First 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:closed
  • Created 2 years ago
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
abidlabscommented, Jan 21, 2022

Ohhhhhh, interesting. Thanks for letting me know!

0reactions
ycdcommented, Jan 21, 2022

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

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found