Django 비동기 도구 Celery 설치

1. 환경 요구
      :centos
      :4.7.1
    :yum
    (200G    ):/data

2. 셀러리 관련 서비스 설치
pip install celery
pip install celery-with-redis
pip install django-celery

3. 편집:settings.py 파일에 다음 내용 추가
#####  celery####
import djcelery
djcelery.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*'

4. 편집:jumpserver/init.py 파일에 다음 내용 추가
from __future__ import absolute_import
from .celery import app as celery_app

5. 편집:jumpserver/celery.py 파일에 다음 내용 추가
from __future__ import absolute_import  
import os
import django
from 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))

6. 편집: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

7.celery 시작
celery -A jumpserver worker -l info

테스트
python manage.py shell

from deploy.tasks import *

result = longtime_addd.delay(1,2)

print 'Task finished? ', result.ready()
print 'Task result: ', result.result

9. 슈퍼바이저 설정과 백엔드 실행celery 결합
  :pip install supervisor
      :echo_supervisord_conf > /etc/supervisord.conf
      :/etc/supervisord.conf      
[program:celery]
command=celery -A jumpserver worker --loglevel=info
directory=/data/xxxxx
stdout_logfile=/data/xxxxx/logs/celery.log
autorestart=true
redirect_stderr=true

  :supervisord -c /etc/supervisord.conf

    :tail -f /data/polaris/logs/celery.log

좋은 웹페이지 즐겨찾기