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

Question:

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
Asked By: huhh hhbhb

||

Answers:

Make sure you run celery beat worker for scheduled tasks:

celery beat --app app.celery

Check the docs here: http://celery.readthedocs.org/en/latest/userguide/periodic-tasks.html#starting-the-scheduler

Answered By: gonz

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
Answered By: davidism

In version 4.1.0 you have to add logger to your task.py file like so:

from celery.utils.log import get_task_logger

logger = get_task_logger(__name__)


@task(name="multiply_two_numbers")
def mul(x, y):
    total = x * (y * random.randint(3, 100))
    #HERE:
    logger.info('Adding {0} + {1}'.format(x, y))
    return total

Stated halfway down in the docs here if you want more info:
http://docs.celeryproject.org/en/latest/userguide/tasks.html

I actually had issues printing mine on command prompt because I was using the wrong command but I found a link to a project which I forked Project

  • (If on Mac ) celery -A Project worker --loglevel=info
  • (If on Windows) celery -A Project worker -l info --pool=solo
Answered By: Codertjay

I know it’s an old thread, but for those running into it… please check your app config. Make sure "TESTING" is set to "False". In my case it’s a Flask APP, also I am updating the celery config with Flask app config

celery.conf.update(app.config)

but just make sure TESTING is not set to "True",

Answered By: Ozzie Ghani
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.