在 Flask 中创建异步任务 [英] Making an asynchronous task in Flask
问题描述
我正在用 Flask 编写一个应用程序,除了 WSGI
是同步和阻塞之外,它运行得非常好.我有一项特别需要调用第三方 API 的任务,该任务可能需要几分钟才能完成.我想打那个电话(实际上是一系列电话)并让它运行.而控制权返回给 Flask.
I am writing an application in Flask, which works really well except that WSGI
is synchronous and blocking. I have one task in particular which calls out to a third party API and that task can take several minutes to complete. I would like to make that call (it's actually a series of calls) and let it run. while control is returned to Flask.
我的观点如下:
@app.route('/render/<id>', methods=['POST'])
def render_script(id=None):
...
data = json.loads(request.data)
text_list = data.get('text_list')
final_file = audio_class.render_audio(data=text_list)
# do stuff
return Response(
mimetype='application/json',
status=200
)
现在,我想要做的是线路
Now, what I want to do is have the line
final_file = audio_class.render_audio()
run 并提供一个回调,当方法返回时执行,而 Flask 可以继续处理请求.这是我需要 Flask 异步运行的唯一任务,我想就如何最好地实现这一点提供一些建议.
run and provide a callback to be executed when the method returns, whilst Flask can continue to process requests. This is the only task which I need Flask to run asynchronously, and I would like some advice on how best to implement this.
我看过 Twisted 和 Klein,但我不确定它们是否矫枉过正,因为也许线程就足够了.或者芹菜是一个不错的选择?
I have looked at Twisted and Klein, but I'm not sure they are overkill, as maybe Threading would suffice. Or maybe Celery is a good choice for this?
推荐答案
我会使用 Celery 为您处理异步任务.您需要安装一个代理作为您的任务队列(推荐使用 RabbitMQ 和 Redis).
I would use Celery to handle the asynchronous task for you. You'll need to install a broker to serve as your task queue (RabbitMQ and Redis are recommended).
app.py
:
from flask import Flask
from celery import Celery
broker_url = 'amqp://guest@localhost' # Broker URL for RabbitMQ task queue
app = Flask(__name__)
celery = Celery(app.name, broker=broker_url)
celery.config_from_object('celeryconfig') # Your celery configurations in a celeryconfig.py
@celery.task(bind=True)
def some_long_task(self, x, y):
# Do some long task
...
@app.route('/render/<id>', methods=['POST'])
def render_script(id=None):
...
data = json.loads(request.data)
text_list = data.get('text_list')
final_file = audio_class.render_audio(data=text_list)
some_long_task.delay(x, y) # Call your async task and pass whatever necessary variables
return Response(
mimetype='application/json',
status=200
)
运行您的 Flask 应用程序,并启动另一个进程来运行您的 celery worker.
Run your Flask app, and start another process to run your celery worker.
$ celery worker -A app.celery --loglevel=debug
我还要参考 Miguel Gringberg 的写以获得更多信息在 Flask 中使用 Celery 的深入指南.
I would also refer to Miguel Gringberg's write up for a more in depth guide to using Celery with Flask.
这篇关于在 Flask 中创建异步任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!