[英]Running Celery as a Flask app with Gunicorn
I'm running Celery as a Flask microservice where it has tasks.py with tasks and manage.py contains the call to run the flask server.我将 Celery 作为 Flask 微服务运行,其中包含带有任务的 tasks.py,而 manage.py 包含运行 Flask 服务器的调用。
This is part of the manage.py这是 manage.py 的一部分
class CeleryWorker(Command):
"""Starts the celery worker."""
name = 'celery'
capture_all_args = True
def run(self, argv):
if "down" in argv:
ret = subprocess.call(
['pkill', '-9', '-f', "my_app.celery"])
sys.exit(ret)
else:
ret = subprocess.call(
['celery', 'worker', '-A', 'my_app.celery'] + argv)
sys.exit(ret)
manager.add_command("celery", CeleryWorker())
I can start the service with either python manage.py runserver
or `celery worker -A my_app.celery and it runs perfectly and registers all tasks in tasks.py.我可以使用
python manage.py runserver
或 `celery worker -A my_app.celery 启动服务,它运行完美并在 tasks.py 中注册所有任务。
But in production, i want to handle multiple requests to this microservice and want to add gunicorn to serve those requests.但在生产中,我想处理对这个微服务的多个请求,并想添加 gunicorn 来满足这些请求。 How do i do it?
我该怎么做?
I'm not able to figure out how i can run both my gunicorn command and celery command together.我无法弄清楚如何同时运行我的 gunicorn 命令和 celery 命令。
Also, i'm running other api services using gunicorn in production from its create_app, since i dont need them to run the celery command.此外,我正在通过其 create_app 在生产中使用 gunicorn 运行其他 api 服务,因为我不需要它们来运行 celery 命令。
Recommend to use Supervisor , which allow you to control a number of processes.推荐使用Supervisor ,它允许您控制多个进程。
step1: pip install supervisor
步骤1:
pip install supervisor
step2: vi supervisord.conf
第二步:
vi supervisord.conf
[program:flask_wsgi]
command=gunicorn -w 3 --worker-class gevent wsgi:app
directory=$SRC_PATH
autostart=true
[program:celery]
command=celery worker -A app.celery --loglevel=info
directory=$SRC_PATH
autostart=true
step3: run supervisord -c supervisord.conf
第三步:运行
supervisord -c supervisord.conf
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.