如何用python芹菜异步任务实现随机任务?

2024-09-29 23:20:52 发布

您现在位置:Python中文网/ 问答频道 /正文

我想在一段时间内的不同时间执行相同的任务。例如。在5分钟。我的代码是这样的

task_period = 5 * 60
task_countdowns = []
last_task_countdown = 0
task_interval = 10
while True:
    last_task_countdown += random.randint(1, 2 * task_interval)
    if last_task_countdown <= task_period:
        task_countdowns.append(last_task_countdown)
    else:
        break
for cd in task_countdowns:
    mytask.apply_async((*args), countdown=cd)

我的问题是:

  1. 在队列中追加多个芹菜任务是否有计数限制?

  2. 有没有更好的方法来解决我的问题?

附言:

  1. 这是一个crontab脚本,每5分钟执行一次

Tags: 代码truetaskif时间cdrandomperiod

热门问题