Celery(4.2):如何获取当前任务的任务ID
Celery (4.2): how to get task id for current task
目标
出于闹钟的目的,我正在尝试 'get' 由 SetAlarmForm
中的 User
创建的特定 Alarm
。
根据同一主题 (Q1, Q2, Q3) 的其他答案,我正在尝试以下行:objx = Alarm.objects.get(id=run_alarm.request.id)
。也许,我遗漏了一些明显的东西或者 Celery 的版本已经更新?
错误
[ERROR/ForkPoolWorker-2] raised unexpected: DoesNotExist('Alarm matching query does not exist')
代码
Models.py
class Alarm(models.Model):
""" Model representing each Alarm """
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
Views.py
class AlarmCreateView(LoginRequiredMixin, CreateView):
""" CreateView for User to create the Alarm object """
model = Alarm
form_class = SetAlarmForm
template_name = 'weather_alarm/set_alarm.html'
login_url = "/login/"
def form_valid(self, form):
self.create_alarm_object(self.request, form)
run_alarm.delay()
return HttpResponseRedirect(self.get_success_url())
Tasks.py
import time
from celery import Celery, shared_task, current_task
from datetime import datetime
from .models import Alarm
@shared_task
def run_alarm():
""" Function to organise the steps for the alarm using Celery """
objx = Alarm.objects.get(id=run_alarm.request.id)
second_countdown = objx.get_alarm_length() # get the length of the alarm, in seconds
time.sleep(second_countdown) # wait for the alarm time
conditions_satisfied = objx.check_conditionals() # check if conditionals are satisfied
if conditions_satisfied == True: # conditions are satified
print("Ring ring!")
return True
else: # conditions aren't satisfied
print("I'm only sleeping!")
return True
解决此问题的最简单方法是使 alarmID
成为传递给您的任务的参数:
Tasks.py
@shared_task
def run_alarm(alarmID):
objx = Alarm.objects.get(id = alarmID)
在视图中调用任务时需要传递此 ID:
Views.py
...
def form_valid(self, form):
#making an assumption about what create_alarm_object returns here; you get the idea
newAlarm = self.create_alarm_object(self.request, form)
run_alarm.delay(newAlarm.id)
注意如何通过在此处将参数传递给 delay
来将参数传递给 run_alarm
。延伸阅读:http://docs.celeryproject.org/en/latest/userguide/calling.html#example
您遇到错误的原因是 request.id
将指向单个异步芹菜任务的任务 ID 运行,而不是警报对象的 ID。延伸阅读:http://docs.celeryproject.org/en/latest/userguide/tasks.html?highlight=request#task-request
目标
出于闹钟的目的,我正在尝试 'get' 由 SetAlarmForm
中的 User
创建的特定 Alarm
。
根据同一主题 (Q1, Q2, Q3) 的其他答案,我正在尝试以下行:objx = Alarm.objects.get(id=run_alarm.request.id)
。也许,我遗漏了一些明显的东西或者 Celery 的版本已经更新?
错误
[ERROR/ForkPoolWorker-2] raised unexpected: DoesNotExist('Alarm matching query does not exist')
代码
Models.py
class Alarm(models.Model):
""" Model representing each Alarm """
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
Views.py
class AlarmCreateView(LoginRequiredMixin, CreateView):
""" CreateView for User to create the Alarm object """
model = Alarm
form_class = SetAlarmForm
template_name = 'weather_alarm/set_alarm.html'
login_url = "/login/"
def form_valid(self, form):
self.create_alarm_object(self.request, form)
run_alarm.delay()
return HttpResponseRedirect(self.get_success_url())
Tasks.py
import time
from celery import Celery, shared_task, current_task
from datetime import datetime
from .models import Alarm
@shared_task
def run_alarm():
""" Function to organise the steps for the alarm using Celery """
objx = Alarm.objects.get(id=run_alarm.request.id)
second_countdown = objx.get_alarm_length() # get the length of the alarm, in seconds
time.sleep(second_countdown) # wait for the alarm time
conditions_satisfied = objx.check_conditionals() # check if conditionals are satisfied
if conditions_satisfied == True: # conditions are satified
print("Ring ring!")
return True
else: # conditions aren't satisfied
print("I'm only sleeping!")
return True
解决此问题的最简单方法是使 alarmID
成为传递给您的任务的参数:
Tasks.py
@shared_task
def run_alarm(alarmID):
objx = Alarm.objects.get(id = alarmID)
在视图中调用任务时需要传递此 ID:
Views.py
...
def form_valid(self, form):
#making an assumption about what create_alarm_object returns here; you get the idea
newAlarm = self.create_alarm_object(self.request, form)
run_alarm.delay(newAlarm.id)
注意如何通过在此处将参数传递给 delay
来将参数传递给 run_alarm
。延伸阅读:http://docs.celeryproject.org/en/latest/userguide/calling.html#example
您遇到错误的原因是 request.id
将指向单个异步芹菜任务的任务 ID 运行,而不是警报对象的 ID。延伸阅读:http://docs.celeryproject.org/en/latest/userguide/tasks.html?highlight=request#task-request