简体   繁体   English

Python Flask表单向后台进程提交函数

[英]Python flask form submit function to background process

I would like to know how can we do the process after the form submit as a background process. 我想知道在将表单提交为后台流程后如何进行处理。

from flask import Flask, g, redirect, url_for
@app.route("/form", methods=['GET', 'POST'])
def form():
if request.method == 'POST':
    #form processing(This block need to do as background)
    #background must have access to the app.* and g.* variables
    return redirect(url_for('app.form_result'))
g.result['page_title'] = 'Form Input'
g.template = 'form.html'
return 'Done'

I see two possible solutions: 我看到两种可能的解决方案:

First, Use APScheduler ( https://pypi.python.org/pypi/APScheduler ) and schedule the process as a single job: 首先,使用APScheduler( https://pypi.python.org/pypi/APScheduler )并将流程安排为一个作业:

from apscheduler.schedulers.background import BackgroundScheduler

def processing_function(app, g):
    pass # Perform your processing here

scheduler = BackgroundScheduler()
scheduler.start()
scheduler.add_job(processing_function, app, g)

Second, Use Celery ( http://www.celeryproject.org/ ). 其次,使用Celery( http://www.celeryproject.org/ )。 Use this if you want to run your background process on a separate server. 如果要在单独的服务器上运行后台进程,请使用此选项。 I won't provide a code sample here, but you'll need to start a Celery worker, as well as a broker, both separately from your web server. 我不会在此处提供代码示例,但是您需要与网络服务器分开地启动Celery worker和代理。 Then, you can pass jobs back to the worker (with arguments), and have the response sent back. 然后,您可以将作业传递回工作程序(带有参数),并将响应发送回去。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM