Django Celery 任务不会完成,并且一直处于挂起状态



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

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

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

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", )

我在模块中创建了一个名为trackerstasks.py文件:

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]

正如你从最后一行看到的,芹菜已经完成了任务,但它只是止步于此。我也似乎无法弄清楚为什么它必须在两者之间按下CTRL ^C。它一直悬而未决。

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

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

相关内容

  • 没有找到相关文章