本文共 2137 字,大约阅读时间需要 7 分钟。
一、环境要求
操作系统版本:centos操作系统内核:4.7.1系统工具:yum数据分区(200G容量以上):/data
二、安装Celery相关服务
pip install celerypip install celery-with-redispip install django-celery
三、编辑:settings.py文件添以下内容
#####配置celery####import djcelerydjcelery.setup_loader()BROKER_URL = 'redis://xx.xx.xx.xx:6379/10'CELERY_RESULT_BACKEND = 'redis://xx.xx.xx.xx:6379/11'CELERY_ACCEPT_CONTENT = ['application/json']CELERY_TASK_SERIALIZER = 'json'CELERY_RESULT_SERIALIZER = 'json'CELERY_TIMEZONE = 'Africa/Nairobi'SECRET_KEY = '8lu*6g0lg)9z!ba+a$ehk)xt)x%rxxgb$i1&022shmi1jcgihb*'
四、编辑:jumpserver/init.py文件添以下内容
from __future__ import absolute_importfrom .celery import app as celery_app
五、编辑:jumpserver/celery.py文件添以下内容
from __future__ import absolute_import import osimport djangofrom celery import Celery from django.conf import settings # set the default Django settings module for the 'celery' program. os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'jumpserver.settings')django.setup()app = Celery('jumpserver')# Using a string here means the worker will not have to # pickle the object when using Windows. app.config_from_object('django.conf:settings') app.autodiscover_tasks(lambda: settings.INSTALLED_APPS) @app.task(bind=True) def debug_task(self): print('Request: {0!r}'.format(self.request))
六、编辑:jumpserver/tasks.py文件添以下内容
from jumpserver import celery_app@celery_app.task()def longtime_addd(x, y): print 'long time task begins' # sleep 5 seconds time.sleep(5) print 'long time task finished' return x + y
七、启动celery
celery -A jumpserver worker -l info
八、测试
python manage.py shellfrom deploy.tasks import *result = longtime_addd.delay(1,2)print 'Task finished? ', result.ready()print 'Task result: ', result.result
九、结合supervisor配置后台运行celery
安装:pip install supervisor生成配置文件:echo_supervisord_conf > /etc/supervisord.conf编辑配置文件:/etc/supervisord.conf添加以下内容[program:celery]command=celery -A jumpserver worker --loglevel=infodirectory=/data/xxxxxstdout_logfile=/data/xxxxx/logs/celery.logautorestart=trueredirect_stderr=true启动:supervisord -c /etc/supervisord.conf查看日志:tail -f /data/polaris/logs/celery.log
转载于:https://blog.51cto.com/aaronsa/2056883