Source

django-celery-about / source / signal.rst

Full commit

Signal

Django Signalのようなもの

ハンドラが同じモジュールなら@task.connect

#: tasks.py
#:
from celery.task import task
from celery.signals import task_sent

@task_sent.connect      #: 自動的に登録
def task_sent_handler(sender=None, task_id=None, task=None, args=None,
                      kwargs=None, \*\*kwds):
    open("/tmp/handlers.txt","a").write(
            'Got signal task_sent for task id %s \n' % (task_id, )
        )
@task
def hello(msg):
    open("/tmp/msg.txt","a").write(msg+"\n")

ハンドラが別モジュールなら signal.connect( signal_handler )

#: handlers.py
#:
def task_success_handler(\*args,\*\*kwargs):
    open("/tmp/task_success.txt","a").write(
        'Task  was successfull completed\n%s\%s\n' % ( str(args),str(kwargs),)
        )
#: tasks.py
#:
from celery.signals import task_success
from app.handlers import *

#: ハンドラをシグナルに接続します。
#: helloがWorkerからコールされるときに、接続されます。
task_success.connect( task_success_handler )

@task
def hello(msg)
    open("/tmp/msg.txt","a").write(msg+"\n")

シグナルいろいろ

Task

  • task_sent
  • task_prerun
  • task_postrun
  • task_success
  • task_failure
  • task_revoked

Worker

  • celeryd_after_setup
  • celeryd_init
  • worker_init
  • worker_ready
  • worker_process_init
  • worker_shutdown

Celerybeat

  • beat_init
  • beat_embedded_init

Eventlet

  • :temr:`eventlet`
  • eventlet_pool_started
  • eventlet_pool_preshutdown
  • eventlet_pool_postshutdown
  • eventlet_pool_apply

Logging

  • setup_logging
  • after_setup_logger
  • after_setup_task_logger