防止烧瓶应用迫使浏览器等待背景过程
我在本地主机上运行以下烧瓶应用程序。然后,我使用http请求在地址/ place_job/上击中该应用程序。之后,我继续浏览器,查找地址/track_site/job/0
预期行为:立即显示“ foo”一词。
实际行为:< / strong>浏览器(edge / chrome)等待 /旋转,直到线完成为止,然后立即显示foo
问题:< / strong>为什么实际行为发生,而不是预期的行为,我该如何解决?最好不要分类芹菜或喜欢的人。
我不明白为什么会发生这种情况。我真正想到的是,如果我删除&lt; script&gt;线在.html文件中。我已经使用螺纹。Thread和theadpool opecutor进行了测试。
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/static/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/static/js/static/js/const.js
const x = 0;
以下是请求/plot_job/flask log/然后导航到/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 -
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 -
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论