Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

157
Views
prevent Flask app from forcing browser to wait for background process

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 -
about 4 years ago · Santiago Trujillo
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!