I run the following flask app on local host. I then hit the app on address/place_job/ with a http request. afterwards i go on my browser and look up address/track_site/job/0
expected behavior: The word "foo" show up immediately.
actual behavior: The browser (edge / Chrome) waits / spins until the thread finishes, and then immediately displays the word foo
Question: Why is the actual behavior happening, instead of the expected behavior, and how do i fix it? Preferably without sorting to celery or the likes.
I don't understand why this happens. What really bugs my mind, is that the expected behavior occurs If i delete the <script> line in the .HTML file. I have tested it with threading.Thread, as well as the threadpool executor from the flask-executor package.
minimal_app.py:
from flask import Flask, render_template
from threading import Thread
from time import time
app = Flask(__name__)
def func():
print("started")
start_time = time()
while time()-start_time<20: pass
print("ended")
@app.route('/place_job/')
def place_job():
Thread(target=func).start()
return "OK"
@app.route('/track_site/job/0')
def track_job():
return render_template('minimal_template.html')
static/templates/minimal_template.html
<!DOCTYPE html>
<html lang="en">
<body>
<h1 id="id1"> Job Page </h1>
<script type="text/javascript" src="/static/js/const.js"></script>
<p id="id2"> foo </p>
</body>
</html>
static/js/const.js
const x = 0;
edit
Below is the flask log after requesting /place_job/ and afterwards navigating to /track_site/job/0 :
127.0.0.1 - - [22/Apr/2022 20:15:04] "GET /place_job HTTP/1.1" 308 -
127.0.0.1 - - [22/Apr/2022 20:15:04] "GET /place_job/ HTTP/1.1" 200 -
started
127.0.0.1 - - [22/Apr/2022 20:15:12] "GET /track_site/job/0 HTTP/1.1" 200 -
ended
127.0.0.1 - - [22/Apr/2022 20:15:24] "GET /static/js/const.js HTTP/1.1" 304 -