-1

I have a time-consuming task in route /fibo and a route /home that return hello.

from flask import Flask, request
from fibo import fib_t
from datetime import datetime

app = Flask(__name__)

@app.route('/home')
def hello():
    return 'hello '

@app.route('/fibo')
def cal_fibo():
    request_rec_t = datetime.now().strftime("%M:%S")
    v, duretion = fib_t(37)
    return f'''
    <div><h1>request recieved by server:</h1>
    <p>{request_rec_t}</p>
    <p>
    <ul>
    <li><b>calculated value</b>:{v}</li> 
    <li><b>that takes</b>: {duretion} seconds</li> 
    <ul>
    </p>
    </div>
    <div>
    <h1>resonse send by server:</h1>
    <p>{datetime.now().strftime("%M:%S")}</p>
    </div>'''


if __name__ == '__main__':
    app.run(debug=True)

the fibo module

import time

def fib(a):
    if a == 1 or a==2:
        return 1
    else:
        return fib(a-1) + fib(a-2)


def fib_t(a):
    t = time.time()
    v = fib(a)
    return v, f'{time.time()-t:.2f}'

if I made two requests to /fibo the second one will start running after the first one finishes his work. but if I send a request to the time-consuming task and another one to /home I will receive the /home route response immediately while still first request isn't resolved. if this is matters, I make the requests from different tabs of the browser.

for example, if I request /fibo in two different tabs. the page starts to load. the captured time for the first request is 10:10 and 10:20(the execution of Fibonacci number is tasked 10 seconds) during this request to be finished, the second tab still is loading. after that the second request is finished i see that the process started at the 10:20(the time that first request finishes process) and finished in 10:29(the execution of Fibonacci number is tasked 9 seconds). but if I request /home while the /fibo in the first tab isn't returned, I will get the response.

why this is happening? my general question is how and by whom the multiple requests are being handled? how can I log the time requests reached the server?

EDIT: if I add another route fibo2 with the same logic as cal_fibo view function, now I can run them concurrently(like /fibo and /home that I showed that are running concurrently, /fibo and /fibo2 are also running concurrently.)

EDIT 2: as shown in the image, the second request was sent after the first request's response was received. why and how did this has happened? (the related TCP handshake of each request has happened close to each other but how and who managed that the related HTTP get request is sent after the first request's response received?)

enter image description here

kankan256
  • 210
  • 1
  • 4
  • 18

1 Answers1

-1

actually you have only one process running with only one thread all the time to enable threading and running more than one process this depends on the context (development or production) .

for development purposes

to enable threading you need to run

app.run(host="your.host", port=4321, threaded=True)

also to enable running more than one process e.g. 3 according to documentation you need to run the following line

app.run(host="your.host", port=4321, processes=3)

for production purposes

when you run the app in production environment it is the responsability of the WSGI gateway (eg. gunicorn) that you configure on the cloud provider like heroku or aws to support the handling of multiple requests.

we use this method in production as it is more robust to crashes and also more efficent.

this was just a summary of the answers in this question

  • I guess the default threaded value is true, also the fibo function is a CPU-bound process so only more processes can solve the problem. I tested what you suggest ```processes=3, threaded=Flase``` that hasn't solved the problem. I will add more details to the question – kankan256 Feb 12 '22 at 15:35
  • I visisted this site https://werkzeug.palletsprojects.com/en/1.0.x/serving/#werkzeug.serving.run_simple and it says the default for threaded=false so try changing it to true and see what happend – Mohammed Ibrahim Feb 12 '22 at 15:52
  • nothing changed. – kankan256 Feb 12 '22 at 15:54
  • you can also try running. (> flask run --with-threads). it is the same as app.run(threaded=True) but sometimes this way works – Mohammed Ibrahim Feb 12 '22 at 16:55
  • still, nothing changed. – kankan256 Feb 12 '22 at 17:01