Source

django-celery-about / source / minimum.rst

Full commit

ミニマムインストール

mkvirtualenv

$ mkvirtualenv soup
$ cdvirtualenv ; mkdir src; cd src

プロジェクト作成

django-admin.py startprojet

(soup)hdknr@wzy:~/ve/soup/src$ cd django-celery-about/
(soup)hdknr@wzy:~/ve/soup/src/django-celery-about$ mkdir sample ; django-admin.py startproject app sample

vim tasks.py

  • celery.task.task デコレータをインポート
  • callableをtaskデコレートした物がタスク
(soup)hdknr@wzy:~/ve/soup/src/django-celery-about$ cd sample/app/
(soup)hdknr@wzy:~/ve/soup/src/django-celery-about/sample/app$ vim tasks.py
# -*- coding: utf-8 -*-
from celery.task import task

@task               # タスク化
def hello(msg):
    ''' こんにちは処理 '''
    open("/tmp/msg.txt","a").write(msg)

vim settings.py

(soup)hdknr@wzy:~/ve/soup/src/django-celery-about/sample/app$ diff settings.py settings.py.org

sqite3を使ってみる:

14,15c14,15
<         'ENGINE': 'django.db.backends.sqlite3', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
<         'NAME': 'soup.sqlite3',                      # Or path to database file if using sqlite3.
---
>         'ENGINE': 'django.db.backends.', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
>         'NAME': '',                      # Or path to database file if using sqlite3.

タイムゾーンとロケール言語設定:

27c27
< TIME_ZONE = 'Asia/Tokyo'
---
> TIME_ZONE = 'America/Chicago'
31c31
< LANGUAGE_CODE = 'ja-jp'---
> LANGUAGE_CODE = 'en-us'

とりあえずadmin有効化:

119c119
<     'django.contrib.admin',
---
>     # 'django.contrib.admin',

Celery を使う

152,158d151
< # - Celery
< INSTALLED_APPS += ('djcelery',"kombu.transport.django", )

Django データベースをブローカーで使う

< BROKER_URL="django://"

Celeryアプリケーションを初期化

< import djcelery
< djcelery.setup_loader()
<

app.tasks.helloをワーカーが認識するように

< INSTALLED_APPS += ('app',)

vim urls.py

admin を有効化

(soup)hdknr@wzy:~/ve/soup/src/django-celery-about/sample/app$ diff urls.py urls.py.org

4,5c4,5
< from django.contrib import admin
< admin.autodiscover()
---
> # from django.contrib import admin
> # admin.autodiscover()
16c16
<     url(r'^admin/', include(admin.site.urls)),
---
>     # url(r'^admin/', include(admin.site.urls)),

python ../manage.py syncdb

(soup)hdknr@wzy:~/ve/soup/src/django-celery-about/sample/app$ python ../manage.py syncdb

Creating tables ...
Creating table auth_permission
Creating table auth_group_permissions
Creating table auth_group
Creating table auth_user_user_permissions
Creating table auth_user_groups
Creating table auth_user
Creating table django_content_type
Creating table django_session
Creating table django_site
Creating table django_admin_log
Creating table celery_taskmeta
Creating table celery_tasksetmeta
Creating table djcelery_intervalschedule
Creating table djcelery_crontabschedule
Creating table djcelery_periodictasks
Creating table djcelery_periodictask
Creating table djcelery_workerstate
Creating table djcelery_taskstate
Creating table djkombu_queue
Creating table djkombu_message

You just installed Django's auth system, which means you don't have any superusers defined.
Would you like to create one now? (yes/no): yes
Username (leave blank to use 'hdknr'): admin
E-mail address: admin@admin.admin
Password:
Password (again):
Superuser created successfully.
Installing custom SQL ...
Installing indexes ...
Installed 0 object(s) from 0 fixture(s)

実行

ワーカー:python ../manage.py celery worker

(soup)hdknr@wzy:~/ve/soup/src/django-celery-about/sample/app$ python ../manage.py celery worker
/home/hdknr/ve/soup/local/lib/python2.7/site-packages/djcelery/loaders.py:116: UserWarning:
Using settings.DEBUG leads to a memory leak,
never use this setting in production environments!
  warnings.warn("Using settings.DEBUG leads to a memory leak, never "

 -------------- celery@wzy v3.0.7 (Chiastic Slide)
---- **** -----
--- * ***  * -- [Configuration]
-- * - **** --- . broker:      django://localhost//
- ** ---------- . app:         default:0x283a950 (djcelery.loaders.DjangoLoader)
- ** ---------- . concurrency: 1 (processes)
- ** ---------- . events:      OFF (enable -E to monitor this worker)
- ** ----------
- *** --- * --- [Queues]
-- ******* ---- . celery:      exchange:celery(direct) binding:celery
--- ***** -----

[2012-08-26 13:57:53,360: WARNING/MainProcess] celery@wzy has started.
[2012-08-26 13:57:53,562: WARNING/PoolWorker-1]
/home/hdknr/ve/soup/local/lib/python2.7/site-packages/djcelery/loaders.py:116: UserWarning:
Using settings.DEBUG leads to a memory leak, never use this setting in production environments!
  warnings.warn("Using settings.DEBUG leads to a memory leak, never "

タスク非同期実行: hello.delay('too hotn')

>>> from app.tasks import *
>>> hello.delay('too hot')
<AsyncResult: 67c61754-d66d-45c3-8a88-554c0a4cb93b>

OK

(soup)hdknr@wzy:~/ve/soup/src/django-celery-about/sample/app$ more /tmp/msg.txt

too hot
_static/title-celery-premier.png