在Flask中创建一个异步任务 [英] Making an asynchronous task in Flask

查看:148
本文介绍了在Flask中创建一个异步任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Flask 中写了一个应用程序,除了 WSGI 是同步和阻塞外,我有一个任务,特别是调用第三方API,这个任务可能需要几分钟才能完成。我想打这个电话(实际上是一系列的电话),让它运行。而控制返回到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



<现在,我想要做的是有行

  final_file = audio_class.render_audio()

code>

在方法返回时运行并提供一个回调函数,而Flask可以继续处理请求。这是我需要Flask异步运行的唯一任务,并且我想就如何最好地实现这一点提供一些建议。我已经看过 Twisted Klein ,但是我不确定它们是否过量,也许线程就足够了。



任何建议都将非常感谢。



编辑



或者也许 Celery 是一个不错的选择吗?

解决方案

我会使用 Celery 为您处理异步任务。你需要安装一个代理作为你的任务队列(推荐使用RabbitMQ和Redis)。

app.py

 从瓶子导入瓶子
从芹菜导入Celery

broker_url = 'amqp:// guest @ localhost'#RabbitMQ任务队列的代理URL
$ b $ app = Flask(__ name__)
celery = celery(app.name,broker = broker_url)
celery.config_from_object('celeryconfig')#celeryconfig.py中的celery配置
$ b @ celery.task(bind = True)
def some_long_task(self,x,y):
$做一些长期任务
...

@ 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)#调用你的异步任务,并传递任何必要的变量iables
返回响应(
mimetype ='application / json',
status = 200

运行你的Flask应用程序,并启动另一个进程来运行你的芹菜工作器。

  $ celery worker -A app.celery --loglevel = debug 

我也会参考Miguel Gringberg的<一个href =http://blog.miguelgrinberg.com/post/using-celery-with-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.

My view looks like:

@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 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. I have looked at Twisted and Klein, but I'm not sure they are overkill, as maybe Threading would suffice.

Any advice would be much appreciated.

EDIT

Or Maybe Celery is a good choice for this?

解决方案

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
    )

Run your Flask app, and start another process to run your celery worker.

$ celery worker -A app.celery --loglevel=debug

I would also refer to Miguel Gringberg's write up for a more in depth guide to using Celery with Flask.

这篇关于在Flask中创建一个异步任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆