我将使用Celery为您处理异步任务。您需要安装代理以充当任务队列(建议使用RabbitMQ和Redis)。
app.py:
from flask import Flaskfrom celery import Celerybroker_url = 'amqp://guest@localhost' # Broker URL for RabbitMQ task queueapp = 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 -A app.celery --loglevel=debug
我还要参考Miguel Gringberg的文章,以获取更深入的将Celery与Flask结合使用的指南。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)