Django芹菜任务没有完成,并一直保持待定

时间:2012-05-07 12:36:19

标签: python django celery amqp django-celery

我安装了运行pip install django-celery的Django Celery bur。这个安装的芹菜和必要的库,例如芹菜和海带。

我将djcelery添加到已安装的应用列表中,并运行syncdbmigrate命令来创建表格。

我已经安装了RabbitMQ并使用以下命令创建了一个用户和vhost:

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

我编辑了我的设置文件以包含以下两行:

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

我在名为tasks.py的模块中创建了一个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

我在终端窗口中启动了Celery守护程序,如下所示:

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

运行此喷出:

[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()

现在我用Python shell打开了一个新的终端窗口,将这样的任务排入队列:

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

任务永远不会完成。它们经常被报告为“待定”。在芹菜守护进程运行的窗口中,我没有看到任何变化。但是,当我按CTRL^C时。我得到了这个额外的文字:

^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]

从最后一行可以看出,Celery已经完成了任务,但它就在那里停止了。此外,我似乎无法弄清楚为什么它必须在其间按CTRL ^C。它一直在等待。

1 个答案:

答案 0 :(得分:7)

固定。这是由于我在Django中的__init__.py,我正在使用Gevent修补一些Python模块。

Gunicorn使用Gevent,似乎Celery使用了Eventlet。 Gevent的猴子修补线程和多处理模块会导致Celery出现打嗝。