我有一个flask应用程序,该应用程序在主页上有一个表单,提交后运行一个python脚本,并在完成后返回结果页面。问题是当在脚本上将参数设置得很高时,脚本将花费30秒钟以上的时间运行,并且heroku在完成之前会超时。 目前,我所提交的是将表单提交到等待页面。
@app.route('/')
def index():
return render_template('index.html')
@app.route('/run', methods=['GET', 'POST'])
def test():
import loopTest
if request.method == 'GET':
loopTest.init()
return 'done'
if request.method == 'POST':
loopTest.episodes_form = request.form.get('episode')
loopTest.max_steps_form = request.form.get('max_steps')
loopTest.per_step_cost = request.form.get('per_step_cost')
loopTest.goal_reward = request.form.get('goal_reward')
loopTest.gamma_form = request.form.get('gamma')
loopTest.epsilon_form = request.form.get('epsilon')
loopTest.epsilon_form_decay = request.form.get('epsilon_decay')
loopTest.alpha_form = request.form.get('alpha')
loopTest.alpha_form_decay = request.form.get('alpha_decay')
return render_template('waiting.html')
@app.route('/success', methods=['GET', 'POST'])
def dealy():
return render_template("result.html")
然后在带有javascript的等待页面上等待脚本完成接收消息的完成。收到此消息后,它将重定向到结果页面。我以为这样可以,但是heroku在30秒后仍然超时。
var request = new XMLHttpRequest();
request.open('GET', '/run');
request.onload=function() {
if (request.status === 200 && request.responseText === 'done') {
// long process finished successfully, redirect user
window.location = '/success';
} else {
// ops, we got an error from the server
alert('Something went wrong.');
}
}
request.onerror = function() {
// ops, we got an error trying to talk to the server
alert('Something went wrong.');
};
request.send();
有什么办法可以解决Ajax的问题?我看过Celery来执行后台任务,但是对于我想做的事情却显得太多了。
作为替代方案,我正在考虑在Google云或AWS上创建虚拟机并从那里运行它。