如何使用 Python Bottle 实现文件准备就绪后自动更新的 "Your file is being prepared. Please wait 1 minute."?
How to implement "Your file is being prepared. Please wait 1 minute." that auto-updates when the file is ready with Python Bottle?
我想做一个 "Your file is being prepared. Please wait 1 minute." 页面,通过让客户端在文件准备就绪时下载文件来自动更新。
显然,这行不通:
from bottle import route, post, get, run, request, view, static_file, redirect
import time
from threading import Thread
def dothejob(i):
time.sleep(10) # the file takes 5 to 60 seconds to be prepared
with open('test.txt', 'w') as f:
f.write('Hello')
return static_file('test.txt', root='.', download=True) # error here
@get('/generatefile')
def generatefile():
i = request.params.get('id', '', type=str)
thread = Thread(target=dothejob, args=(i, ))
thread.start()
return "Your file is being prepared. Please wait 1 minute."
run(host='0.0.0.0', port=80, debug=True, quiet=True)
因为当 dothejob
returns:
时 HTTP 请求不再存在
RuntimeError: Request context not initialized.
当文件在服务器上就绪时,如何正确执行这样的自动更新页面?
注:
我想在这里避免websockets
(我已经将它用于更复杂的项目:聊天等,有时它在某些不接受的连接上不起作用它 + 此处不在主题的其他原因)并寻求尽可能简单的解决方案。
我想知道它是否真的需要像 Update and render a value from Flask periodically's answer 那样的 AJAX 轮询,或者是否有更简单的解决方案。
我试着写了一个解决方案(不是 100% 确定这样的轮询是否真的有必要)。当然 HTML+JS 必须移动到真正的 HTML / JS 文件中,但这里我为了一个最小的例子而将它保持得很小:
from bottle import get, run, request, static_file
import time
from threading import Thread
import os
def dothejob(i):
time.sleep(5) # the file takes 5 to 60 seconds to be prepared
with open('test.txt', 'w') as f:
f.write('Hello')
@get('/<filename>')
def static(filename):
return static_file(filename, root='.', download=True)
@get('/isready')
def isready():
return '1' if os.path.exists('test.txt') else '0'
@get('/')
def generatefile():
i = request.params.get('id', '', type=str)
thread = Thread(target=dothejob, args=(i, ))
thread.start()
return """Your file is being prepared. Please wait 5 seconds (do not reload the page)...
<script>
poll = function () {
var xhr = new XMLHttpRequest();
xhr.open('GET', '/isready');
xhr.onreadystatechange = function() {
if (xhr.readyState === 4 && xhr.status === 200) {
if (xhr.responseText == 1)
document.write('Your file is now ready: <a href="/test.txt">download link</a>');
else
setTimeout(poll, 1000);
}
}
xhr.send(null);
}
setTimeout(poll, 1000);
</script>
"""
run(host='0.0.0.0', port=80, debug=True, quiet=True)
我想做一个 "Your file is being prepared. Please wait 1 minute." 页面,通过让客户端在文件准备就绪时下载文件来自动更新。
显然,这行不通:
from bottle import route, post, get, run, request, view, static_file, redirect
import time
from threading import Thread
def dothejob(i):
time.sleep(10) # the file takes 5 to 60 seconds to be prepared
with open('test.txt', 'w') as f:
f.write('Hello')
return static_file('test.txt', root='.', download=True) # error here
@get('/generatefile')
def generatefile():
i = request.params.get('id', '', type=str)
thread = Thread(target=dothejob, args=(i, ))
thread.start()
return "Your file is being prepared. Please wait 1 minute."
run(host='0.0.0.0', port=80, debug=True, quiet=True)
因为当 dothejob
returns:
RuntimeError: Request context not initialized.
当文件在服务器上就绪时,如何正确执行这样的自动更新页面?
注:
我想在这里避免
websockets
(我已经将它用于更复杂的项目:聊天等,有时它在某些不接受的连接上不起作用它 + 此处不在主题的其他原因)并寻求尽可能简单的解决方案。我想知道它是否真的需要像 Update and render a value from Flask periodically's answer 那样的 AJAX 轮询,或者是否有更简单的解决方案。
我试着写了一个解决方案(不是 100% 确定这样的轮询是否真的有必要)。当然 HTML+JS 必须移动到真正的 HTML / JS 文件中,但这里我为了一个最小的例子而将它保持得很小:
from bottle import get, run, request, static_file
import time
from threading import Thread
import os
def dothejob(i):
time.sleep(5) # the file takes 5 to 60 seconds to be prepared
with open('test.txt', 'w') as f:
f.write('Hello')
@get('/<filename>')
def static(filename):
return static_file(filename, root='.', download=True)
@get('/isready')
def isready():
return '1' if os.path.exists('test.txt') else '0'
@get('/')
def generatefile():
i = request.params.get('id', '', type=str)
thread = Thread(target=dothejob, args=(i, ))
thread.start()
return """Your file is being prepared. Please wait 5 seconds (do not reload the page)...
<script>
poll = function () {
var xhr = new XMLHttpRequest();
xhr.open('GET', '/isready');
xhr.onreadystatechange = function() {
if (xhr.readyState === 4 && xhr.status === 200) {
if (xhr.responseText == 1)
document.write('Your file is now ready: <a href="/test.txt">download link</a>');
else
setTimeout(poll, 1000);
}
}
xhr.send(null);
}
setTimeout(poll, 1000);
</script>
"""
run(host='0.0.0.0', port=80, debug=True, quiet=True)