Source

django-celery-about / source / eta.rst

Full commit

ETA:Estimated Time of Arrival

pytz

  • エラーが発生します
ImproperlyConfigured: Timezones requires the pytz library

:term:`pytz` をインストールしましょう。

$ pip install pytz

django.utils.timezone

now()

#:settings.py

USE_TZ = True
TIME_ZONE = 'Asia/Tokyo'
>>> from datetime import datetime
>>> datetime.now()
datetime.datetime(2012, 9, 11, 3, 9, 53, 540212)
>>> from django.utils.timezone import now
>>> now()
datetime.datetime(2012, 9, 10, 18, 10, 8, 644891, tzinfo=<UTC>)

get_default_timezone()

>>> from django.utils.timezone import get_default_timezone
>>> get_default_timezone()
<DstTzInfo 'Asia/Tokyo' CJT+9:00:00 STD>
  • こだわりの :term:`CJT` (Central Japan Time)だよ!

make_aware()/make_naive()

>>> from django.utils.timezone import *
>>> from datetime import datetime
>>> dt
datetime.datetime(2012, 8, 30, 12, 12, 2, 898955)

>>> make_aware(dt,get_current_timezone())
datetime.datetime(2012, 8, 30, 12, 12, 2, 898955, tzinfo=<DstTzInfo 'Asia/Tokyo' JST+9:00:00 STD>)

>>> make_naive(make_aware(dt,get_current_timezone()),get_current_timezone() )
datetime.datetime(2012, 8, 30, 12, 12, 2, 898955)

eta:タスクを指定した時刻で実行

#: メッセージを記録するモデル
from django.db import models

class Message(models.Model):
    created = models.DateTimeField('created', auto_now_add=True)
    message = models.CharField('Message',max_length=500)
 from celery.task import task
 from celery.signals import task_sent,task_success

 from django.utils.timezone import now

 from app.models import Message

 #: タスクのエンキューを記録
 @task_sent.connect
 def task_sent_handler(sender=None, task_id=None, task=None, args=None,
                       kwargs=None, \**kwds):
     Message(message =
             'task_sent_handler:Got signal task_sent for task id %s \n' % (task_id, )
         ).save()

 #: タスクの正常終了を記録
 @task_sent.connect
 def task_success_handler(\*args,**kwargs):
     Message(message=
             'task_success_handler:Task  was successfull completed\n%s\%s\n' % ( str(args),str(kwargs),)
     ).save()

 #: タスク本体
@task
def note(msg="",*args,**kwargs):
    Message(message="note:%s" % msg).save()

10秒後に実行

>>> from app.tasks import *
>>> from django.utils.timezone import now
>>> from datetime import timedelta

>>> note.apply_async(args=("xxxxx",),eta=now() + timedelta(seconds=10))
<AsyncResult: eef89029-33e1-4c54-a3ec-349f34bc3bab>
sqlite> select * from app_message order by created desc;

3|2012-08-30 02:53:44.091310|task_success_handler:Task  was successfull completed
()\{'signal': <Signal: Signal>, 'result': None, 'sender': <@task: app.tasks.note>}

2|2012-08-30 02:53:44.015552|note:xxxxx
1|2012-08-30 02:53:34.474674|task_sent_handler:Got signal task_sent for task id None

countdown : タスクを指定した秒数後に実行

例:20秒後に実行

>>> note.apply_async(args=("xxxxx",),countdown=20)
<AsyncResult: 0886d69e-a155-4545-b200-3a5c78f4690e>
sqlite> select * from app_message order by created desc;

6|2012-08-30 03:05:44.044916|task_success_handler:Task  was successfull completed
()\{'signal': <Signal: Signal>, 'result': None, 'sender': <@task: app.tasks.note>}

5|2012-08-30 03:05:44.020992|note:xxxxx
4|2012-08-30 03:05:24.583609|task_sent_handler:Got signal task_sent for task id None

用語