4

我安装了 Django Celery bur running pip install django-celery。这安装了 celery 和必要的库,例如 celery 和 kombu。

我添加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", )

我在我的模块中创建了一个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

任务永远不会完成。它们经常被报告为“待处理”。在 celery 守护进程运行的窗口中,我没有看到任何变化。但是,当我按下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在两者之间按下它。它一直处于未决状态。

4

1 回答 1

7

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

Gunicorn 使用 Gevent,而 Celery 似乎使用 Eventlet。Gevent 对线程和多处理模块的猴子补丁会导致 Celery 出现问题。

于 2012-05-07T13:41:20.660 回答