首页 > 解决方案 > Django 中的 Apscheduler Job 执行两次

问题描述

计划的作业执行两次,以纳秒为单位。我在task.py中有如下任务

def print_hello():
   print("time-->",datetime.datetime.now())
   print("hello")

def print_world():
   print("time-->",datetime.datetime.now())
   print("hello")

scheduler = BackgroundScheduler()
scheduler1 = BackgroundScheduler()

从 app 文件夹中添加 app.py 包含以下代码行..

from django.apps import AppConfig
from datetime import date
class ExploitDataConfig(AppConfig):
     name = 'exploit_data' 

     def ready(self):
    
         from exploit_data import task   

         task.scheduler.add_job(task.print_hello, 'interval', minutes=5)
         task.scheduler1.add_job(task.print_world, 'interval', minutes=5
         task.scheduler.start()
         task.scheduler1.start()

我希望它每隔 5 分钟 只执行一次。任何帮助都是可观的..

标签: pythondjangodjango-rest-frameworkschedulerapscheduler

解决方案


也许这可以帮助:

python manage.py runserver --noreload

推荐阅读