FastAPI requests to internal endpoint
See original GitHub issueI was working on a project recently and was experiencing problems with FastAPI interacting with the requests library. The goal I’m trying to achieve is to have a health_status endpoint function hit the health version endpoint as well. I have recreated the problem with 2 smaller projects, a simple fastapi and a simple flask. Flask is able to achieve the goal, but Fastapi freezes up when trying to do it.
Here is the code using flask. When hitting the “/health/status” endpoin it should return {“message”: “Hello World”} however it freezes up the entire api. It doesn’t even allow me to hit the root endpoint afterwards unless I rerun the program. However, I am able to hit the root endpoint if I hit it before calling “/health/status."
Code
import uvicorn
from fastapi import FastAPI, Request
import requests
app = FastAPI(debug=True)
@app.get("/")
async def root():
return {"message": "Hello World"}
@app.get("/health/status")
async def health():
resp = requests.get(url="http://127.0.0.1:8000/")
return resp.text
if __name__ == "__main__":
uvicorn.run(app, host="127.0.0.1", port=8000)
Similarly, the code using flask is listed below. This works just fine, where if I hit “/health/status” then it will return “Hello World”
from flask import Flask
import requests
app = Flask(__name__)
@app.route('/')
def hello_world():
return 'Hello World'
@app.route('/health/status')
def health_status():
resp = requests.get(url="http://127.0.0.1:5000/")
return resp.text
if __name__ == '__main__':
app.run()
Description
As mentioned above, instead of returning “Hello World” like it should, it just ties up the whole api when using FastAPI.
Environment
- OS: [e.g. Linux / Windows / macOS]: Windows
- FastAPI Version [e.g. 0.3.0]: “==0.63.0”
- Python version: “==3.6.8”
Issue Analytics
- State:
- Created 3 years ago
- Comments:6 (4 by maintainers)
Top Related StackOverflow Question
Use
httpxinstead ofrequests, would make your call asynchronous and stop the blocking.The thing is that you’re calling a blocking synchronous function on a
coroutineand it will block the event loop. Possible solutions are:health()synchronous i.e. remove theasync. This way it will run in another thread.root()in this case).Approach 2 performs better.