Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

159
Visualizações
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
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda