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.

Limit FastApi to process only 1 Request at a time.

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

#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

image 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:closed
  • Created 2 years ago
  • Comments:9 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
tiangolocommented, Nov 12, 2022

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.

1reaction
tr-giscommented, Mar 16, 2022

did you try locking the threads?. Seems to work . I tried something like this:

#main.py
from fastapi import FastAPI
import threading

lock = threading.Lock()

app = FastAPI()

@app.post('/limit')
def func():
    lock.acquire()
    # Doing some work that may take about 10 - 30 minutes
    lock.release()
    return result

Note that I changed function def from async to normal def.

Read more comments on GitHub >

github_iconTop 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 >

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