Search code examples
pythoncelery

Print statement in Celery scheduled task doesn't appear in terminal


When I run celery -A tasks2.celery worker -B I want to see "celery task" printed every second. Currently nothing is printed. Why isn't this working?

from app import app
from celery import Celery
from datetime import timedelta

celery = Celery(app.name, broker='amqp://guest:@localhost/', backend='amqp://guest:@localhost/')
celery.conf.update(CELERY_TASK_RESULT_EXPIRES=3600,)

@celery.task
def add(x, y):
    print "celery task"
    return x + y

CELERYBEAT_SCHEDULE = {
    'add-every-30-seconds': {
        'task': 'tasks2.add',
        'schedule': timedelta(seconds=1),
        'args': (16, 16)
    },
}

This is the only output after staring the worker and beat:

[tasks]
  . tasks2.add

[INFO/Beat] beat: Starting...
[INFO/MainProcess] Connected to amqp://guest:**@127.0.0.1:5672//
[INFO/MainProcess] mingle: searching for neighbors
[INFO/MainProcess] mingle: all alone

Solution

  • You wrote the schedule, but didn't add it to the celery config. So beat saw no scheduled tasks to send. The example below uses celery.config_from_object(__name__) to pick up config values from the current module, but you can use any other config method as well.

    Once you configure it properly, you will see messages from beat about sending scheduled tasks, as well as the output from those tasks as the worker receives and runs them.

    from celery import Celery
    from datetime import timedelta
    
    celery = Celery(__name__)
    celery.config_from_object(__name__)
    
    @celery.task
    def say_hello():
        print('Hello, World!')
    
    CELERYBEAT_SCHEDULE = {
        'every-second': {
            'task': 'example.say_hello',
            'schedule': timedelta(seconds=5),
        },
    }
    
    $ celery -A example.celery worker -B -l info
    
    [tasks]
      . example.say_hello
    
    [2015-07-15 08:23:54,350: INFO/Beat] beat: Starting...
    [2015-07-15 08:23:54,366: INFO/MainProcess] Connected to amqp://guest:**@127.0.0.1:5672//
    [2015-07-15 08:23:54,377: INFO/MainProcess] mingle: searching for neighbors
    [2015-07-15 08:23:55,385: INFO/MainProcess] mingle: all alone
    [2015-07-15 08:23:55,411: WARNING/MainProcess] celery@netsec-ast-15 ready.
    [2015-07-15 08:23:59,471: INFO/Beat] Scheduler: Sending due task every-second (example.say_hello)
    [2015-07-15 08:23:59,481: INFO/MainProcess] Received task: example.say_hello[2a9d31cb-fe11-47c8-9aa2-51690d47c007]
    [2015-07-15 08:23:59,483: WARNING/Worker-3] Hello, World!
    [2015-07-15 08:23:59,484: INFO/MainProcess] Task example.say_hello[2a9d31cb-fe11-47c8-9aa2-51690d47c007] succeeded in 0.0012782540870830417s: None