如何执行“您的文件正在准备中。请等待1分钟。”使用Python Bottle准备好文件后,该文件会自动更新吗?

时间:2018-06-20 09:54:19

标签: python multithreading asynchronous bottle

我想做一个“您的文件正在准备中。请等待1分钟。” 页面,该页面会通过使客户端在文件准备就绪时下载文件来自动更新。

显然,这是行不通的:

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返回时HTTP请求不再存在:

  

RuntimeError:请求上下文未初始化。

在服务器上准备好文件后如何正确处理此类自动更新页面?

注意:

  • 我想在这里避免使用websockets(我已经将它用于更复杂的项目:聊天等,有时在某些不接受它的连接上不起作用+其他原因不在这里),并寻求最简单的解决方案。

  • 我想知道它是否真的需要像Update and render a value from Flask periodicallyanswer这样的AJAX轮询,或者是否有更简单的解决方案。

1 个答案:

答案 0 :(得分:0)

我试图写一个解决方案(不是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)