이전 : First Steps with Celery


샐러리 튜토리얼 따라한 기록.

사용 환경 : Ubuntu 14.04 / python 3.5 / PyCharm 5.0.3 / Celery 3.1


Next Steps

http://docs.celeryproject.org/en/latest/getting-started/next-steps.html#next-steps


First Seps with Celery는 최소한의 것이었습니다. 이 가이드에서는 샐러리가 제공하는 것들을 더 디테일하게 실행시켜 볼 것입니다. 사용자의 애플리케이션과 라이브러리를 위해 샐러리를 추가하는 방법을 포함해서요.


1. 내 프로그램에서 샐러리 사용하기

프로젝트 레이아웃 :

proj/__init__.py

    /celery.py

    /tasks.py



주의 ) 위 튜토리얼처럼 하면 오류난다.

- proj

 ㄴ proj

    ㄴtasks.py
    ㄴcelery.py

    ㄴceleryconfig.py


이렇게 프로젝트 폴더 안에 폴더를 하나 만들고 그 안에 샐러리 파일들이 들어가있어야 한다.




실제 내 폴더 : SaleCalendar - roadshops 아래에 샐러리 파일들이 있다.




proj/celery.py

from __future__ import absolute_import

from celery import Celery

app = Celery('proj',
             broker='amqp://',
             backend='amqp://',
             include=['proj.tasks'])

# Optional configuration, see the application user guide.
app.conf.update(
    CELERY_TASK_RESULT_EXPIRES=3600,
)

if __name__ == '__main__':
    app.start()

이 모듈에서는 Celery 인스턴스를 만들었습니다. 프로젝트에서 샐러리를 사용하기 위하여 사용자는 이 인스턴스들을 import합니다.

- broker는 사용할 브로커의 URL

- backend는 사용할 백엔드.

- include는 워커가 시작할 때 import할 모듈들의 목록. 사용자는 여기에 tasks 모듈을 넣을 필요가 있고, 그러면 워커는 우리의 tasks를 찾을 수 있습니다.



proj/tasks.py

from __future__ import absolute_import

from proj.celery import app


@app.task
def add(x, y):
    return x + y


@app.task
def mul(x, y):
    return x * y


@app.task
def xsum(numbers):
    return sum(numbers)



2. 워커 실행

>> celery -A celerytest worker -l info


멈추기

Ctrl+C


백그라운드에서

>> celery multi start w1 -A proj -l info


재시작

>> celery multi restart w1 -A proj -l info


멈추기

>> celery multi stop w1 -A proj -l info


stop은 비동기적이기 때문에 워커를 기다리지 않는다. 현재 task가 완료된 후 멈추고 싶으면 :

기다렸다가 멈추기

>> celery multi stopwait w1 -A proj -l info