Search code examples
pythondjangoceleryamqpdjango-celery

Django Celery tasks dont finish and constantly remain pending


I installed Django Celery bur running pip install django-celery. This installed celery and the necessary libraries e.g. celery and kombu.

I added djcelery to my list of installed apps and ran the syncdb and migrate commands to create the tables.

I've installed RabbitMQ and created a user and vhost using these commands:

rabbitmqctl add_user trakklr trakklr
rabbitmqctl add_vhost /trakklr 
rabbitmqctl set_permissions -p /trakklr trakklr ".*" ".*" ".*"

I edited my settings file to contain these two lines:

BROKER_URL = "amqp://trakklr:trakklr@localhost:5672//trakklr"
CELERY_IMPORTS = ("app.trackers.tasks", )

I created a tasks.py file in the my modules called trackers:

from celery.task import task

@task(name="trackers.tasks.add")
def add(x, y):
    print("In task %s" % add.request.id)
    return x + y

I started the Celery daemon in a terminal window like this:

python manage.py celeryd -l debug --discard --settings=production

Running this spews this:

[2012-05-07 15:30:44,681: DEBUG/MainProcess] Start from server, version: 8.0, properties: {u'information': u'Licensed under the MPL.  See http://www.rabbitmq.com/', u'product': u'RabbitMQ', u'copyright': u'Copyright (C) 2007-2011 VMware, Inc.', u'capabilities': {}, u'platform': u'Erlang/OTP', u'version': u'2.7.1'}, mechanisms: [u'PLAIN', u'AMQPLAIN'], locales: [u'en_US']
[2012-05-07 15:30:44,682: DEBUG/MainProcess] Open OK! known_hosts []
[2012-05-07 15:30:44,683: DEBUG/MainProcess] using channel_id: 1
[2012-05-07 15:30:44,684: DEBUG/MainProcess] Channel open
[2012-05-07 15:30:44,686: WARNING/MainProcess] discard: Erased 3 messages from the queue.
[2012-05-07 15:30:44,687: WARNING/MainProcess]

 -------------- celery@Webserver v2.5.3
---- **** -----
--- * ***  * -- [Configuration]
-- * - **** ---   . broker:      amqp://trakklr@localhost:5672//trakklr
- ** ----------   . loader:      djcelery.loaders.DjangoLoader
- ** ----------   . logfile:     [stderr]@DEBUG
- ** ----------   . concurrency: 1
- ** ----------   . events:      OFF
- *** --- * ---   . beat:        OFF
-- ******* ----
--- ***** ----- [Queues]
 --------------   . celery:      exchange:celery (direct) binding:celery


[Tasks]
  . celery.backend_cleanup
  . celery.chord
  . celery.chord_unlock
  . trackers.tasks.add

[2012-05-07 15:30:44,693: DEBUG/MainProcess] [Worker] Loading modules.
[2012-05-07 15:30:44,696: DEBUG/MainProcess] [Worker] Claiming components.
[2012-05-07 15:30:44,697: DEBUG/MainProcess] [Worker] Building boot step graph.
[2012-05-07 15:30:44,697: DEBUG/MainProcess] [Worker] New boot order: ['queues', 'pool', 'mediator', 'beat', 'autoreloader', 'timers', 'state-db', 'autoscaler', 'consumer']
[2012-05-07 15:30:44,699: DEBUG/MainProcess] Starting celery.concurrency.processes.TaskPool...
[2012-05-07 15:30:44,711: DEBUG/MainProcess] created semlock with handle 3077668864
[2012-05-07 15:30:44,711: DEBUG/MainProcess] created semlock with handle 3077664768
[2012-05-07 15:30:44,712: DEBUG/MainProcess] created semlock with handle 3077660672
[2012-05-07 15:30:44,712: DEBUG/MainProcess] created semlock with handle 3077656576
[2012-05-07 15:30:44,713: DEBUG/MainProcess] created semlock with handle 3077652480
[2012-05-07 15:30:44,713: DEBUG/MainProcess] created semlock with handle 3077648384
[2012-05-07 15:30:44,713: DEBUG/MainProcess] created semlock with handle 3077644288
[2012-05-07 15:30:44,714: DEBUG/MainProcess] created semlock with handle 3077640192
[2012-05-07 15:30:44,714: DEBUG/MainProcess] created semlock with handle 3077636096
[2012-05-07 15:30:44,734: DEBUG/MainProcess] worker handler starting
[2012-05-07 15:30:44,738: DEBUG/MainProcess] result handler starting
[2012-05-07 15:30:44,744: DEBUG/PoolWorker-1] Closed channel #1
[2012-05-07 15:30:44,746: INFO/PoolWorker-1] child process calling self.run()

Now I opened a new terminal window with the Python shell to enqueue tasks like this:

>>> from app.trackers.tasks import add
>>> task = add.delay(1, 4)
>>> task
<AsyncResult: c3298494-de50-41b5-8a3a-77017a1298f2>
>>> task.state
u'PENDING'
>>> task.result

The tasks never finish. They are constantly reported as "pending". In the window the celery daemon running, I don't see any changes. However, when I press CTRL^C. I get this additional text:

^C[2012-05-07 15:31:55,541: DEBUG/MainProcess] result handler got IOError(4, 'Interrupted system call') -- exiting
[2012-05-07 15:31:55,544: DEBUG/MainProcess] celery.concurrency.processes.TaskPool OK!
[2012-05-07 15:31:55,546: DEBUG/MainProcess] Starting celery.worker.mediator.Mediator...
[2012-05-07 15:31:55,552: DEBUG/MainProcess] celery.worker.mediator.Mediator OK!
[2012-05-07 15:31:55,553: DEBUG/MainProcess] Starting celery.worker.consumer.Consumer...
[2012-05-07 15:31:55,556: WARNING/MainProcess] celery@Webserver has started.
[2012-05-07 15:31:55,557: DEBUG/MainProcess] Consumer: Re-establishing connection to the broker...
[2012-05-07 15:31:55,570: DEBUG/MainProcess] Start from server, version: 8.0, properties: {u'information': u'Licensed under the MPL.  See http://www.rabbitmq.com/', u'product': u'RabbitMQ', u'copyright': u'Copyright (C) 2007-2011 VMware, Inc.', u'capabilities': {}, u'platform': u'Erlang/OTP', u'version': u'2.7.1'}, mechanisms: [u'PLAIN', u'AMQPLAIN'], locales: [u'en_US']
[2012-05-07 15:31:55,571: DEBUG/MainProcess] Open OK! known_hosts []
[2012-05-07 15:31:55,571: DEBUG/MainProcess] Consumer: Connection established.
[2012-05-07 15:31:55,572: DEBUG/MainProcess] using channel_id: 1
[2012-05-07 15:31:55,573: DEBUG/MainProcess] Channel open
[2012-05-07 15:31:55,576: DEBUG/MainProcess] basic.qos: prefetch_count->4
[2012-05-07 15:31:55,576: DEBUG/MainProcess] using channel_id: 2
[2012-05-07 15:31:55,577: DEBUG/MainProcess] Channel open
[2012-05-07 15:31:55,589: DEBUG/MainProcess] Consumer: Starting message consumer...
[2012-05-07 15:31:55,590: DEBUG/MainProcess] Consumer: Ready to accept tasks!
[2012-05-07 15:31:55,591: INFO/MainProcess] Got task from broker: trackers.tasks.add[c3298494-de50-41b5-8a3a-77017a1298f2]

As you can see from the last line, Celery has got the task but it just stops there. Also i can't seem to figure out why it have to press that CTRL ^C in between. It's constantly pending.


Solution

  • Fixed. This was due to my __init__.py in Django inside which I was patching some Python modules using Gevent.

    Gunicorn uses Gevent and it seems that Celery uses Eventlet. Gevent's monkey patching of thread and multiprocessing modules causes hiccups in Celery.