11번째 줄: |
11번째 줄: |
| == 기초 사용법 == | | == 기초 사용법 == |
| 사람마다 사용하는 방식이 다르다. | | 사람마다 사용하는 방식이 다르다. |
| + | |
| + | 다음 방식은 규모가 있는 프로젝트를 다룰 때 사용하기 적합한 방식. |
| {| class="wikitable" | | {| class="wikitable" |
| !과정 | | !과정 |
| !설명 | | !설명 |
| !방법 | | !방법 |
| + | |- |
| + | |사전설정 |
| + | |장고에서 celery_tutorial이라는 프로젝트가 만들어져 있다 가정하고 진행한다. |
| + | | |
| |- | | |- |
| |라이브러리 인스턴스 만들기 | | |라이브러리 인스턴스 만들기 |
| |settings.py가 있는 디렉토리에 celery.py를 생성 우측과 같이 입력한다. | | |settings.py가 있는 디렉토리에 celery.py를 생성 우측과 같이 입력한다. |
| + | |
| + | |
| + | 만약 app.autodiscover_tasks()를 사용하지 않는다면 인스턴스를 지정할 때 app = Celery('celery_tutorial', include=['앱이름.tasks']) 형태로 기입하면 포함된 tasks.py만 찾는다. |
| |<syntaxhighlight lang="python"> | | |<syntaxhighlight lang="python"> |
| from __future__ import absolute_import, unicode_literals | | from __future__ import absolute_import, unicode_literals |
26번째 줄: |
35번째 줄: |
| | | |
| # set the default Django settings module for the 'celery' program. | | # set the default Django settings module for the 'celery' program. |
− | os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'celery_tutorial.settings') | + | os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'celery_tutorial.settings') # 설정파일 위치. |
| | | |
− | app = Celery('celery_tutorial') | + | app = Celery('celery_tutorial') # 보통 settings.py 파일이 있는 디렉토리 이름을 넣는다. |
| | | |
| # Using a string here means the worker doesn't have to serialize | | # Using a string here means the worker doesn't have to serialize |
34번째 줄: |
43번째 줄: |
| # - namespace='CELERY' means all celery-related configuration keys | | # - namespace='CELERY' means all celery-related configuration keys |
| # should have a `CELERY_` prefix. | | # should have a `CELERY_` prefix. |
− | app.config_from_object('django.conf:settings', namespace='CELERY') | + | app.config_from_object('django.conf:settings', namespace='CELERY') # 장고의 설정을 읽고, 네임스페이스를 설정한다. |
| | | |
| # Load task modules from all registered Django app configs. | | # Load task modules from all registered Django app configs. |
− | app.autodiscover_tasks() # 선택사항. 자동으로 하위 앱의 task.py를 탐색한다. | + | app.autodiscover_tasks() # 선택사항. 자동으로 하위 앱의 tasks.py를 탐색한다. |
| </syntaxhighlight> | | </syntaxhighlight> |
| |- | | |- |
51번째 줄: |
60번째 줄: |
| __all__ = ('celery_app',) # 샐러리 | | __all__ = ('celery_app',) # 샐러리 |
| </syntaxhighlight> | | </syntaxhighlight> |
| + | |- |
| + | |확장라이브러리 |
| + | |라이브러리를 설치한다. |
| + | |pip install django-celery-results |
| |- | | |- |
| |settings.py 설정 | | |settings.py 설정 |
| |메시지브로커 주소 등 설정값을 넣어준다. | | |메시지브로커 주소 등 설정값을 넣어준다. |
| |<syntaxhighlight lang="python"> | | |<syntaxhighlight lang="python"> |
− | CELERY_BROKER_URL = 'redis://localhost:6379' # redis 주소값 | + | INSTALLED_APPS = ( # 이 안에 확장 라이브러리를 등록한다. |
| + | 'django_celery_results', |
| + | ) |
| + | |
| + | CELERY_BROKER_URL = 'redis://localhost:6379' # redis 주소값' |
| + | # django-celery-result 백엔드 설정. |
| + | CELERY_RESULT_BACKEND = 'django-db' |
| + | CELERY_CAHCE_BACKEND = 'django-cache' |
| </syntaxhighlight> | | </syntaxhighlight> |
| + | |- |
| + | |마이그레이션 |
| + | |확장 라이브러리 사용을 위해 DB테이블 생성. |
| + | |python manage.py migrate celery_results |
| |- | | |- |
| |task 등록 | | |task 등록 |
| |일을 등록한다. | | |일을 등록한다. |
− | 위에서 app.autodiscover_tasks()를 사용했다면 각 앱의 task.py 안에 넣어주면 작동한다. | + | 위에서 app.autodiscover_tasks()를 사용했다면 각 앱의 tasks.py 안에 넣어주면 작동한다. |
− | |@app.task(bind=True) | + | |사용하고자 하는 앱 안에서 tasks.py로 등록한다.<syntaxhighlight lang="python"> |
| + | from __future__ import absolute_import, unicode_literals |
| + | from celery import shared_task |
| + | |
| + | @shared_task |
| + | def 함수(변수): |
| + | 명령 |
| + | return 결과 |
| + | </syntaxhighlight>app.autodiscover_tasks()를 사용하지 않았다면 아래와 같이 사용한다.<syntaxhighlight lang="python"> |
| + | from __future__ import absolute_import, unicode_literals |
| + | from .celery import app |
| + | |
| + | @app.task |
| + | def 함수(변수): |
| + | 명령 |
| + | return 결과 |
| + | </syntaxhighlight>@app.task(bind=True) |
| def debug_task(self): | | def debug_task(self): |
| print('Request: {0!r}'.format(self.request)) | | print('Request: {0!r}'.format(self.request)) |
| + | |
| + | bind=True 옵션을 사용하면 task 인스턴스를 참조해 자신의 request를 쉽게 가져올 수 있다는데... 찾아볼 필요가 있겠다. |
| |- | | |- |
| |샐러리 실행 | | |샐러리 실행 |
− | | | + | |[데몬으로 시작하는 법은 따로 있나보다.] |
| |celery -A settings.py가있는폴더.celery worker --loglevel=info | | |celery -A settings.py가있는폴더.celery worker --loglevel=info |
| |} | | |} |