When I use Django Celery apply_async with eta, it does the job immediately

kni*_*oid 8 python django celery celery-task django-celery

i looked at celery documentation and trying something from it but it not work like the example. maybe i'm wrong at some point, please give me some pointer if i'm wrong about the following code

in views.py i have something like this:

class Something(CreateView):
  model = something

  def form_valid(self, form):
    obj = form.save(commit=False)
    number = 5
    test_limit = datetime.now() + timedelta(minutes=5)
    testing_something.apply_async((obj, number), eta=test_limit)
    obj.save()
Run Code Online (Sandbox Code Playgroud)

and in celery tasks i wrote something like this:

@shared_task()
def add_number(obj, number):
    base = Base.objects.get(id=1)
    base.add = base.number + number
    base.save()
return obj
Run Code Online (Sandbox Code Playgroud)

my condition with this code is the celery runs immediately after CreateView runs, my goal is to run the task add_number once in 5 minutes after running Something CreateView. Thank You so much

Edit:

  1. i've tried change the eta into countdown=180 but it still running function add_number immediately. i also tried longer countdown but still running immediately
  2. i've tried @johnmoustafis answer but still the same, the task run immediately
  3. i've also tried @dana answer but it still the same, the task run immediately

Joh*_*fis 5

Celery 默认使用 UTC 时间。
如果您的时区“落后”于 UTC (UTC - HH:MM),则该datetime.now()调用将返回一个“落后”于 UTC 的时间戳,从而导致您的任务立即执行。

您可以datetime.utcnow()改用:

test_limit = datetime.utcnow() + timedelta(minutes=5)
Run Code Online (Sandbox Code Playgroud)

由于您使用的是 django,因此存在另一种选择:

如果您已经设置了USE_TZ = Truesetting.py,您已经启用了Django的时区设置,你可以使用timezone.now(),而不是datetime.utcnow()

from django.utils import timezone

...

test_limit = timezone.now() + timedelta(minutes=5)
Run Code Online (Sandbox Code Playgroud)