Limit FastApi to process only 1 Request at a time.
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
#main.py
from fastapi import FastAPI
app = FastAPI()
@app.post('/limit')
async def func():
# Doing some work that may take about 10 - 30 minutes
return result
Description
I want to limit the requests at this endpoint. As this endpoint will take some time to finish, so once a request comes and it starts being processed, any other request that is made before the finishing of first request gets discarded(Or can get 429 response).
In short I want to add a Limit of 1 REQUEST AT A TIME.
I’ve seen slow-api, fastapi-limiter, and other limiters. But all I got from them was to limit rate according to some time like (10 requests/sec) or similar.
Hope i can get any workaround on this!!
Thanks…
Operating System
macOS
Operating System Details
No response
FastAPI Version
0.73.0
Python Version
Python 3.9.10
Additional Context
https://nordicapis.com/everything-you-need-to-know-about-api-rate-limiting/This bottleneck seems to do the trick, but I don’t have a clue of implementing this in fastapi.
Issue Analytics
- State:
- Created 2 years ago
- Comments:9 (3 by maintainers)
Top Results From Across the Web
Limit Fastapi to process only 1 request at a time and after ...
I'm trying to limit Fastapi to process 1 request at a time. And while that request is being executed, other requests gets response...
Read more >Limit Fastapi to process only 1 request at a time and after ...
I'm trying to limit Fastapi to process 1 request at a time. And while that request is being executed, other requests gets response...
Read more >tiangolo/fastapi - Gitter
I am wondering about how to limit to n requests instead of just 1 request as in the case of lock? Moritz E....
Read more >Using the Request Directly - FastAPI
Use the Request object directly For that you need to access the request directly. By declaring a path operation function parameter with the...
Read more >Concurrency and async / await - FastAPI
Note: You can mix def and async def in your path operation functions as much as you need and define each one using...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Thanks for the help here everyone! 👏 🙇
If that solves the original problem, then you can close this issue @arjit1804 ✔️
For others with other problems, it’s probably better to create a new issue, following the template, creating a minimal, self-contained example that I can copy paste and replicate the problem, etc.
did you try locking the threads?. Seems to work . I tried something like this:
Note that I changed function def from async to normal def.