My FastAPI is not responding to request after waiting for a while, and requires Ctrl+C to restart
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, BackGroundTasks
app = FastAPI()
@app.post("/create", status_code=201, response_model=schemas.TaskResponseModel)
async def create_tasks(
task: schemas.TrainingRequestModel, background_tasks: BackgroundTasks
):
logging.info("{} Start executing background task at: ".format(datetime.now()))
background_tasks.add_task(train_model, task) # train model at background
response = schemas.TaskResponseModel(task_id=task.task_id)
logging.info("Created response")
print("endpoint response:", response)
return response
Description
I’m building API application for machine learning models, and have endpoint “create” to input the training data then execute training.
After my Web APP send the request, my FastAPI waits for a long time and seems not receiving response. During when I send a simple request by typing http://127.0.0.1:5000/docs
in my browser, and it waits a long time to run but not showing the swagger document.
I assume FastAPI is working in background, maybe receiving the request but facing some problem, so I press Ctrl+C to stop the program. What strange happens is that as long as I press the terminate (Ctrl+C), the program starts running and receive my request to “create” endpoint. It’s like FastAPI is stuck in some problem while receiving message, and I need to pause the work to make it continue.
Other from that problem mentioned above, when I sometimes typing http://127.0.0.1:5000/docs
in the browser after idle for a while, it waits for a long time with no response, and same I have to press Ctrl+C to then it continues running.
Please help me with this problem, any advice is appreciate. Thank you!
Operating System
Windows
Operating System Details
Windows 10 Pro
FastAPI Version
0.68.0
Python Version
3.8.10
Additional Context
No response
Issue Analytics
- State:
- Created 2 years ago
- Comments:9 (1 by maintainers)
Top GitHub Comments
No.
I guess you’re running the server with a single worker. Try to add more workers.
I tried with removing async and wrapping up the task in a regular function. It started to lock the DB.