1

Currently my Flask app only processes one request at one time. Any request has to wait for previous request to finish before being processed and it is not a good user experience.

While I do not want to increase the number of requests the Flask app can processed at one time, how is it possible to return a Server Busy message immediately when the next request comes in before the previous request finishes?

I have tried out the threading method below, but can only get both 'Server busy message' and "Proper return message" after 10 seconds.

import threading
from contextlib import ExitStack

busy = threading.Lock()
@app.route("/hello")
def hello():
    if busy.acquire(timeout = 1):
        return 'Server busy message'

    with ExitStack() as stack:
        stack.callback(busy.release)
        # simulate heavy processing
        time.sleep(10)

    return "Proper return message"
cnhh8
  • 11
  • 2
  • 1
    maybe you want to also have a look here for some info. https://stackoverflow.com/questions/42325105/flask-processing-requests-1-by-1 – SNicolaou Oct 13 '20 at 10:30
  • Thank you for your reply! I have tried out the threading solution previously as updated in my question, but is still not able to get an immediate reply. I can only get the reply after the previous request has finished processing. – cnhh8 Oct 13 '20 at 11:26
  • then might be worth trying the semaphore e.g. posix_ipc, demo on the github site: https://github.com/osvenskan/posix_ipc/tree/master/demo and documentation here: http://semanchuk.com/philip/posix_ipc/ – SNicolaou Oct 13 '20 at 11:47

0 Answers0