Django Celery bur running をインストールしpip install django-celery
ました。これにより、セロリと、セロリや昆布などの必要なライブラリがインストールされました。
djcelery
インストール済みアプリのリストに追加し、コマンドsyncdb
とmigrate
コマンドを実行してテーブルを作成しました。
RabbitMQ をインストールし、次のコマンドを使用してユーザーと vhost を作成しました。
rabbitmqctl add_user trakklr trakklr
rabbitmqctl add_vhost /trakklr
rabbitmqctl set_permissions -p /trakklr trakklr ".*" ".*" ".*"
設定ファイルを編集して、次の 2 行を含めました。
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 シェルで新しいターミナル ウィンドウを開き、次のようなタスクをキューに入れました。
>>> 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
。それは常に保留中です。