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?)