Python 带有后台线程的烧瓶应用程序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22615475/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
flask application with background threads
提问by San
I am creating a flask application, for one request I need to run some long running job which is not required to wait on the UI. I will create a thread and send a message to UI. The thread will calculate and update the database. But, UI will see a message upon submit. Below is my implementation, but it is running the thread and then sending the output to UI which is not I prefer. How can I run this thread in the background?
我正在创建一个烧瓶应用程序,对于一个请求,我需要运行一些不需要等待 UI 的长时间运行的作业。我将创建一个线程并向 UI 发送消息。该线程将计算并更新数据库。但是,UI 在提交时会看到一条消息。下面是我的实现,但它正在运行线程,然后将输出发送到我不喜欢的 UI。如何在后台运行此线程?
@app.route('/someJob')
def index():
t1 = threading.Thread(target=long_running_job)
t1.start()
return 'Scheduled a job'
def long_running_job
#some long running processing here
How can I make thread t1 to run the background and immediately send message in return?
如何让线程 t1 运行后台并立即发送消息作为回报?
采纳答案by rdegges
The best thing to do for stuff like this is use a message broker. There is some excellent software in the python world meant for doing just this:
最好的办法是使用消息代理。python 世界中有一些优秀的软件就是为了做到这一点:
- Celery (http://www.celeryproject.org/), and
- RQ (http://python-rq.org/).
- 芹菜(http://www.celeryproject.org/),和
- RQ ( http://python-rq.org/)。
Both are excellent choices.
两者都是极好的选择。
It's almost never a good idea to spawn a thread the way you're doing it, as this can cause issues processing incoming requests, among other things.
按照您的方式生成线程几乎从来都不是一个好主意,因为这可能会导致处理传入请求等问题。
If you take a look at the celery or RQ getting started guides, they'll walk you through doing this the proper way!
如果您查看 celery 或 RQ 入门指南,它们会引导您以正确的方式完成此操作!
回答by Denys Synashko
Try this example, tested on Python 3.4.3 / Flask 0.11.1
试试这个例子,在 Python 3.4.3 / Flask 0.11.1 上测试
from flask import Flask
from time import sleep
from concurrent.futures import ThreadPoolExecutor
# DOCS https://docs.python.org/3/library/concurrent.futures.html#concurrent.futures.ThreadPoolExecutor
executor = ThreadPoolExecutor(2)
app = Flask(__name__)
@app.route('/jobs')
def run_jobs():
executor.submit(some_long_task1)
executor.submit(some_long_task2, 'hello', 123)
return 'Two jobs were launched in background!'
def some_long_task1():
print("Task #1 started!")
sleep(10)
print("Task #1 is done!")
def some_long_task2(arg1, arg2):
print("Task #2 started with args: %s %s!" % (arg1, arg2))
sleep(5)
print("Task #2 is done!")
if __name__ == '__main__':
app.run()
回答by turdus-merula
If you'd like to execute the long-running operation within the flask application context, then it's a bit easier to (as opposed to using ThreadPoolExecutor, taking care of exceptions):
如果您想在flask application context 中执行长时间运行的操作,那么它更容易一些(与使用ThreadPoolExecutor,处理异常相反):
- Define a command line for your application (
cli.py) - because all web applications should have an adminclianyway. subprocess.Popen(no wait) the command line in a web request.
- 为您的应用程序定义一个命令行 (
cli.py) - 因为cli无论如何所有 Web 应用程序都应该有一个管理员。 subprocess.Popen(无需等待)Web 请求中的命令行。
For example:
例如:
# cli.py
import click
import yourpackage.app
import yourpackage.domain
app = yourpackage.app.create_app()
@click.group()
def cli():
pass
@click.command()
@click.argument('foo_id')
def do_something(foo_id):
with app.app_context():
yourpackage.domain.do_something(foo_id)
if __name__ == '__main__':
cli.add_command(do_something)
cli()
Then,
然后,
# admin.py (flask view / controller)
bp = Blueprint('admin', __name__, url_prefix='/admin')
@bp.route('/do-something/<int:foo_id>', methods=["POST"])
@roles_required('admin')
def do_something(foo_id):
yourpackage.domain.process_wrapper_do_something(foo_id)
flash("Something has started.", "info")
return redirect(url_for("..."))
And:
和:
# domain.py
import subprocess
def process_wrapper_do_something(foo_id):
command = ["python3", "-m", "yourpackage.cli", "do_something", str(foo_id)]
subprocess.Popen(command)
def do_something(foo_id):
print("I am doing something.")
print("This takes some time.")
回答by Dylan Anthony
Check out Flask-Executorwhich uses concurrent.futures in the background and makes your life very easy.
查看Flask-Executor,它在后台使用 concurrent.futures,让您的生活变得非常轻松。
from flask_executor import Executor
executor = Executor(app)
@app.route('/someJob')
def index():
executor.submit(long_running_job)
return 'Scheduled a job'
def long_running_job
#some long running processing here
This not only runs jobs in the background but gives them access to the app context. It also provides a way to store jobs so users can check back in to get statuses.
这不仅在后台运行作业,而且使他们可以访问应用程序上下文。它还提供了一种存储作业的方法,以便用户可以重新检查以获取状态。

