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()
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
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
我的条件是芹菜在CreateView运行后立即运行,我的目标是在运行某个CreateView后每5分钟运行一次任务add_number。非常感谢你
Edit:
编辑:
- i've tried change the
eta
intocountdown=180
but it still running functionadd_number
immediately. i also tried longer countdown but still running immediately - 我尝试过将eta改为countdown=180,但是它仍然会立即运行函数add_number。我也尝试了更长的倒计时,但仍然立即运行
- i've tried @johnmoustafis answer but still the same, the task run immediately
- 我尝试了@johnmoustafis的回复,但还是一样,任务会立即运行
- i've also tried @dana answer but it still the same, the task run immediately
- 我也尝试了@dana的答案,但是它仍然是一样的,任务会立即运行
3 个解决方案
#1
1
Celery by default uses UTC time.
If your timezone is "behind" the UTC (UTC - HH:MM) the datetime.now()
call will return a timestamp which is "behind" UTC, thus causing your task to be executed immediately.
芹菜默认使用UTC时间。如果您的时区在UTC (UTC - HH:MM)后面,则now()调用将返回一个时间戳,该时间戳位于UTC后面,因此将立即执行任务。
You can use datetime.utcnow()
instead:
您可以使用datetime.utcnow()代替:
test_limit = datetime.utcnow() + timedelta(minutes=5)
Since you are using django, there exist another option:
由于您正在使用django,因此还有另一个选项:
If you have set the USE_TZ = True
in your setting.py
, you have enabled the django timezone settings and you can use timezone.now()
instead of datetime.utcnow()
:
如果在设置中设置了USE_TZ = True。py,您已经启用了django时区设置,并且可以使用timezone.now()而不是datetime.utcnow():
from django.utils import timezone
...
test_limit = timezone.now() + timedelta(minutes=5)
#2
0
You might have the CELERY_ALWAYS_EAGER=True
setting.
您可能有CELERY_ALWAYS_EAGER=True设置。
Could you also post your configuration and the Celery version you are using?
你能把你的配置和芹菜版本也贴出来吗?
Here you might find some useful information.
在这里你可能会发现一些有用的信息。
#3
0
'test_limit' variable hasn't got timezone information. So Celery will understand eta param as UTC time.
'test_limit'变量没有时区信息。所以芹菜会把eta param理解为UTC时间。
Please use modified code:
请使用修改后的代码:
class Something(CreateView):
model = something
def form_valid(self, form):
obj = form.save(commit=False)
number = 5
test_limit = datetime.now()
test_limit = test_limit.replace(tzinfo=tz.tzlocal())
test_limit = test_limit + timedelta(minutes=5)
testing_something.apply_async((obj, number), eta=test_limit)
obj.save()
#1
1
Celery by default uses UTC time.
If your timezone is "behind" the UTC (UTC - HH:MM) the datetime.now()
call will return a timestamp which is "behind" UTC, thus causing your task to be executed immediately.
芹菜默认使用UTC时间。如果您的时区在UTC (UTC - HH:MM)后面,则now()调用将返回一个时间戳,该时间戳位于UTC后面,因此将立即执行任务。
You can use datetime.utcnow()
instead:
您可以使用datetime.utcnow()代替:
test_limit = datetime.utcnow() + timedelta(minutes=5)
Since you are using django, there exist another option:
由于您正在使用django,因此还有另一个选项:
If you have set the USE_TZ = True
in your setting.py
, you have enabled the django timezone settings and you can use timezone.now()
instead of datetime.utcnow()
:
如果在设置中设置了USE_TZ = True。py,您已经启用了django时区设置,并且可以使用timezone.now()而不是datetime.utcnow():
from django.utils import timezone
...
test_limit = timezone.now() + timedelta(minutes=5)
#2
0
You might have the CELERY_ALWAYS_EAGER=True
setting.
您可能有CELERY_ALWAYS_EAGER=True设置。
Could you also post your configuration and the Celery version you are using?
你能把你的配置和芹菜版本也贴出来吗?
Here you might find some useful information.
在这里你可能会发现一些有用的信息。
#3
0
'test_limit' variable hasn't got timezone information. So Celery will understand eta param as UTC time.
'test_limit'变量没有时区信息。所以芹菜会把eta param理解为UTC时间。
Please use modified code:
请使用修改后的代码:
class Something(CreateView):
model = something
def form_valid(self, form):
obj = form.save(commit=False)
number = 5
test_limit = datetime.now()
test_limit = test_limit.replace(tzinfo=tz.tzlocal())
test_limit = test_limit + timedelta(minutes=5)
testing_something.apply_async((obj, number), eta=test_limit)
obj.save()