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.

My FastAPI is not responding to request after waiting for a while, and requires Ctrl+C to restart

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, 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:open
  • Created 2 years ago
  • Comments:9 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
Kludexcommented, Sep 3, 2021

No.

I guess you’re running the server with a single worker. Try to add more workers.

0reactions
upeshphablecommented, Dec 17, 2021

I tried with removing async and wrapping up the task in a regular function. It started to lock the DB.

Read more comments on GitHub >

github_iconTop Results From Across the Web

FastAPI is not quitting when pressing Ctr+c - Stack Overflow
I've read about this problem in using uvicorn and I found the below code snippet to resolve that: # Add the below code...
Read more >
First Steps - FastAPI
INFO: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit). That line shows the URL where your app is being served, in your local...
Read more >
Debugging - FastAPI
Run the debugger with the option " Python: Current File (Integrated Terminal) ". It will then start the server with your FastAPI code,...
Read more >
SQL (Relational) Databases with Peewee - FastAPI
And as your app starts to handle more and more clients at the same time, the waiting time in a single request needs...
Read more >
FastAPI
FastAPI framework, high performance, easy to learn, fast to code, ready for production.
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