Source

django-celery-about / source / enqueue_task.rst

Full commit

タスクの呼び出し

Celeryを介さない

your_task(...)

>>> from app.tasks import *
>>> note('give me a break')

EagerResult : 同期呼び出し

your_task.apply(*args,**kwargs,...)

>>> note.apply(['give me a break'])
<EagerResult: c4d3685b-21f2-4d26-9724-8dcbd06f2697>

eager

AsyncResult : 非同期呼び出し

your_task.apply_async(*args,**kwargs,...)

>>> hello.apply_async(['give me a break'])
<AsyncResult: a894d32b-e3aa-44fe-bf24-77913923d402>

your_task.deley(*arg,**kwargs) (ショートカット)

>>> hello.delay('give me a break')
<AsyncResult: 1e2daa02-cc12-45c5-b967-f08fd5863efc>

強制的に同期呼び出し: settings.CELERY_ALWAYS_EAGER = True

unittestの時など

# settings.py
CELERY_ALWAYS_EAGER = True
>>> hello.apply_async(['give me a break'])
<EagerResult: 8e10d59b-2fa0-4a0e-b878-7f34ce49d714>

send_task:タスク名で呼び出し

settings.CELERY_ALWAYS_EAGER = True は無視されます(必ず非同期)

>>> from celery.execute import send_task
>>> send_task('app.tasks.hello',args=['give me a break'])
/home/hdknr/ve/soup/local/lib/python2.7/site-packages/celery/app/base.py:194:
AlwaysEagerIgnored: CELERY_ALWAYS_EAGER has no effect on send_task
'CELERY_ALWAYS_EAGER has no effect on send_task'))
<AsyncResult: c2f78725-aa74-4bdf-8d8a-4a69fe9d8746>

>>> from celery import current_app as C
>>> C.conf['CELERY_ALWAYS_EAGER']
True