【问题标题】:Flask won't continue to execute after redirectFlask 在重定向后不会继续执行
【发布时间】:2017-03-10 06:37:01
【问题描述】:

我是 Flask 的新手。我想创建一个简单的项目,我可以将文件上传到网络,调用exe程序来处理文件并输出进程的日志。我尝试对http://flask.pocoo.org/docs/0.12/patterns/fileuploads/ 的文件上传模板稍作修改,以实现我想要的功能。

我设法浏览并将文件保存到我的目录,但我无法运行 processing_call 方法。但是,我尝试刷新页面,发现可以运行exe程序。这意味着 processing_call 有效。但我想要的是调用exe而不需要刷新页面。

这是我的代码:

@app.route('/', methods=['GET', 'POST'])
def upload_file():
if request.method == 'POST':
    # check if the post request has the file part
    if 'file' not in request.files:
        flash('No file part')
        return redirect(request.url)
    file = request.files['file']
    # if user does not select file, browser also
    # submit a empty part without filename
    if file.filename == '':
        flash('No selected file')
        return redirect(request.url)
    if file and allowed_file(file.filename):
        filename = secure_filename(file.filename)
        file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
        return redirect(url_for('uploaded_file',
                                filename=filename))
        processing_file()
return render_template('uploadfile.html',subprocess_output=processing_file())

【问题讨论】:

    标签: python redirect flask


    【解决方案1】:

    在 Python 语言中,return 语句之后的代码永远不会起作用(据我所知,在所有语言中都是一样的)。所以如果你需要做某事,在return之前执行。如果你不想在执行函数时阻塞用户,你需要任务队列(https://www.fullstackpython.com/task-queues.html)来异步调用它。

    【讨论】:

      猜你喜欢
      • 2012-04-24
      • 1970-01-01
      • 2012-01-19
      • 1970-01-01
      • 2011-12-29
      • 2021-02-26
      • 2015-06-15
      • 2018-09-10
      • 2016-06-07
      相关资源
      最近更新 更多