英文:
Image not loading before Javascript+Python function ends
问题
以下是您的内容的中文翻译:
我正在使用Python和Eel与本地网站进行前端通信。我的代码应该在长时间运行的Python函数中在网页上显示一张图片。尽管HTML中正确的时间存在<img>
标签,但图片只在Python函数执行后出现。
这是我的JavaScript代码:
eel.expose(set_image);
function set_image() {
document.getElementById("zoom-animate").innerHTML = '<img src="temp.png">';
}
function generate() {
let source = document.getElementById("source").value;
let keyword = document.getElementById("keyword").value;
eel.generate(source, keyword);
}
这是我的Python代码:
@eel.expose
def generate(source, keyword):
# 需要长时间执行的代码 1
eel.set_image()
# 需要长时间执行的代码 2
英文:
I'm using python with eel to communicate with a local website as frontend. My code should display an image on a web page while in the middle of a long python function. The image only appears after the python function is executed, even though <img>
is present in the HTML at the correct time.
Here's my Javascript code:
eel.expose(set_image);
function set_image() {
document.getElementById("zoom-animate").innerHTML = '<img src="temp.png">';
}
function generate() {
let source = document.getElementById("source").value;
let keyword = document.getElementById("keyword").value;
eel.generate(source, keyword);
}
And my Python code:
@eel.expose
def generate(source, keyword):
# code that takes a long time to execute 1
eel.set_image()
# code that takes a long time to execute 2
答案1
得分: 0
Your generate()
function should call set_image()
and immediately return
. That way the WSGI server can immediately send a 200
document to the web client. As it stands, the web server is patiently waiting for your function to return.
Use a celery worker to complete the "long time to execute 2" task in the background.
英文:
Your generate()
function should call set_image()
and immediately return
.
That way the
WSGI
server can immediately send a 200
document to the web client.
As it stands, the web server is patiently waiting for your function to return.
Use a celery
worker to complete the "long time to execute 2" task
in the background.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论