Timofey Kargin
Timofey Kargin

Reputation: 181

Flask: how to ignore new requests if last one is not done yet

I am trying to build REST API with only one call.

Sometimes it takes up to 30 seconds for a program to return a response. But if user thinks that service is lagging - he makes a new call and my app returns response with error code 500 (Internal Server Error).

For now it is enough for me to block any new requests if last one is not ready. Is there any simple way to do it?

I know that there is a lot of queueing managers like Celery, but I prefer not to overload my app with any large dependencies/etc.

Upvotes: 0

Views: 1423

Answers (1)

alberto vielma
alberto vielma

Reputation: 2342

You could use Flask-Limiter to ignore new requests from that remote address. pip install Flask-Limiter Check this quickstart:

from flask import Flask
from flask_limiter import Limiter
from flask_limiter.util import get_remote_address

app = Flask(__name__)
limiter = Limiter(
    app,
    key_func=get_remote_address,
    default_limits=["200 per day", "50 per hour"]
)
@app.route("/slow")
@limiter.limit("1 per day")
def slow():
    return "24"

@app.route("/fast")
def fast():
    return "42"

@app.route("/ping")
@limiter.exempt
def ping():
    return "PONG"

As you can see, you could ignore the remote IP address for a certain amount of time meanwhile you finish the process you´re running

DOCS

Check these two links:

Upvotes: 1

Related Questions