环境celery(4.0.2)+rabbitmq +redis(3.2.1) +kombu(4.1.0)
安装 pip install jobtastic==2.1.1
from jobtastic import JobtasticTask
"""1、新建一个任务 继承JobtasticTask 类
2、重写def calculate_result(self, **kwargs): 计算结果方法,这里写任务内容,并计算进度
3、在任务中更新进度 self.update_progress(已完成,总量,更新频率)
"""
class TestJobtasticTask(JobtasticTask):
queue = "my_queue123" # 队列
# significant_kwargs 里的参数 用来标记 是否是同一个结果
significant_kwargs = [
# ('numerators', str),
# ('denominators', str),
]
herd_avoidance_timeout = 60 # Shouldn't take more than 60 seconds
# 持续存储结果 -1表示不存储结果
cache_duration = 0 # Cache these results forever. Math is pretty stable.
def calculate_result(self, **kwargs):
results = []
qs = MyModels.objects.filter(is_deleted=False)
s = time.time()
count_to_do = len(qs)
step = 0
# Only actually update the progress in the backend every 10 operations
update_frequency = 2 # 更新进度频率
for q in qs:
step += 1
results.append(step/count_to_do)
print('任务执行中...')
# Let's let everyone know how we're doing 更新进度
self.update_progress(
step,
count_to_do,
update_frequency=update_frequency,
)
return results
# 起 worker 队列
celery -A projectName worker -l info -Q my_queue123
# JobtasticTask 结果查看
>>> res =TestJobtasticTask.delay_or_fail()
>>> res.state
'PROGRESS'
>>> res.ready()
False
>>> res.result
{'time_remaining': 23.494531202316267, 'progress_percent': 83.33333333333334}
>>> res.result
{'time_remaining': 23.494531202316267, 'progress_percent': 83.33333333333334}
>>> res.result
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]
Jobtastic基本情况:
JobtasticTask 继承了celery.task.Task
celery.task.Task 这个类不在推荐使用了