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.

FastAPI is not using enough threads

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

..

Description

When I send too many requests to my fastapi application it spends most of the time doing nothing which I think is because of threading switching and there being not enough threads in the pool. However, the CPU usage is nowhere near 100% and the memory is less than 10% I am wondering how can I force fastapi to use more threads ? I could not find any resource to set the thread to a higher number in the latest version and what is the default number of threads ?

Operating System

Linux

Operating System Details

No response

FastAPI Version

0.73

Python Version

3.9

Additional Context

No response

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
Kludexcommented, Jun 29, 2022

Just in case…

import anyio
from fastapi import FastAPI


app = FastAPI()


@app.on_event("startup")
async def startup():
    limiter = anyio.to_thread.current_default_thread_limiter()
    limiter.total_tokens = 100


@app.get("/")
async def home():
    print(anyio.to_thread.current_default_thread_limiter().total_tokens)  # you'll see 100
0reactions
Kludexcommented, Feb 15, 2022

You mentioned that you’re using 0.73…

Just do what I said above but instead of changing the value of total_tokens, just print it. You’ll see the default value.

Read more comments on GitHub >

github_iconTop Results From Across the Web

FastAPI with uvicorn won't allow thread usage to exceed ~65%
Under normal operation, this library will use 8 threads for inference, and will max out all 8 threads 100%. This is the desired...
Read more >
SQL (Relational) Databases - FastAPI
By default SQLite will only allow one thread to communicate with it, assuming that each thread would handle an independent request. This is...
Read more >
FastAPI and cooperative multi-threading - Ebury Labs
We are using FastAPI, Pydantic, and Kubernetes to build microservices. ... and hope that all of them are not clogged at the same...
Read more >
FastAPI - The Good, the Bad and the Ugly - Infolytx
Django and FastAPI respond slower than usual in their first API call. Flask always stays consistent but is much slower than the other...
Read more >
tiangolo/fastapi - Gitter
@madkote You said that you had Flask with 4 threads (and 4 cpu/cores ... there a way to solve this without having to...
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