有什么方法可以使@periodic_task 运行 仅在调用时自动在项目启动时自动 运行 吗?

Any way to make @periodic_task run on call only,it runs automatically on project starts?

有什么方法可以让 periodic_task 变成 运行 只在通话时,我看到 Pingit() 在我 运行 我的 django-app python manage.py runserver

@periodic_task(run_every=crontab(minute="*/1"),options={"task_id":task_name})
    def Pingit():
        print('Every Minute Im Called')

我想让它成为 运行 周期性任务,只有当我通过 Pingit 调用它时。

您最好为此使用 @task 并让它在执行后重新排队,例如:

@app.task
def pingit(count=0):
    if count < 60 * 24 * 7:  # 7 days in minutes
        print('Every Minute Im Called')

        # Queue a new task to run in 1 minute
        pingit.apply_async(kwargs={'count': count + 1}, countdown=60)

# Start the task manually
pingit.apply_async()

如果您需要向函数添加位置参数,您可以使用 args 指定。例如,要传递 name 参数:

@app.task
def pingit(name, count=0):
    if count < 60 * 24 * 7:  # 7 days in minutes
        print('Every Minute Im Called')

        # Queue a new task to run in 1 minute
        pingit.apply_async(args=[name], kwargs={'count': count + 1}, countdown=60)

# Start the task manually
pingit.apply_async(args=['MyName'])