Garvit Jain
Garvit Jain

Reputation: 635

celery task not processed by celery

total celery and django noob here, so sorry if the problem is trivial. Basically the problem is that any function defined by @app.task is not being processed by celery, it just runs normally as if celery isn't there.

My celery_app.py file is -

from __future__ import absolute_import
import os
from celery import Celery
from django.conf import settings

# set the default Django settings module for the 'celery' program.
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'project.settings')

app = Celery(broker=settings.CELERY_BROKER_URL)
app.config_from_object('django.conf:settings')
app.autodiscover_tasks()

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

While my tasks.py file is -

from project.celery_app import app

@app.task
def mytask():
    ...

I get the following output on running celery in the terminal -

 -------------- celery@LAPTOP v4.1.0 (latentcall)
---- **** -----
--- * ***  * -- Windows-10-10.0.16299-SP0 2017-12-20 19:27:24
-- * - **** ---
- ** ---------- [config]
- ** ---------- .> app:         __main__:0x229ce2884e0
- ** ---------- .> transport:   amqp://user:**@localhost:5672/myvhost
- ** ---------- .> results:     disabled://
- *** --- * --- .> concurrency: 8 (solo)
-- ******* ---- .> task events: OFF (enable -E to monitor tasks in this         worker)
--- ***** -----
 -------------- [queues]
                .> celery           exchange=celery(direct) key=celery


[tasks]
  . account.tasks.mytask

[2017-12-20 19:27:24,085: INFO/MainProcess] Connected to     amqp://user:**@127.0.0.1:5672/myvhost
[2017-12-20 19:27:24,101: INFO/MainProcess] mingle: searching for neighbors
[2017-12-20 19:27:25,126: INFO/MainProcess] mingle: all alone
[2017-12-20 19:27:25,141: WARNING/MainProcess]     c:\programdata\anaconda2\envs\myenv\lib\site-    packages\celery\fixups\django.py:202: UserWarning: Using settings.DEBUG leads to     a memory leak, never use this setting in production environments!
  warnings.warn('Using settings.DEBUG leads to a memory leak, never '
[2017-12-20 19:27:25,141: INFO/MainProcess] celery@LAPTOP- ready.

So my task is known to celery, but it doesn't do anything about it. The task runs on a button click, and using -loglevel=debug it is seen that celery isn't affected by it. I am using RabbitMQ as broker, celery 4.1.0, python3 and django-1.10.5. Any help would be greatly appreciated!

Upvotes: 2

Views: 2243

Answers (2)

Lahiru
Lahiru

Reputation: 186

.delay() is actually a shorcut method. If want to provide additional options you have to use .apply_async()

official doc can be found here: http://docs.celeryproject.org/en/latest/userguide/calling.html

Upvotes: 1

Garvit Jain
Garvit Jain

Reputation: 635

As I had thought, a simple mistake. Just needed to change mytask() to mytask.delay() and celery started receiving it.

Upvotes: 3

Related Questions