这是我的scheduler.py文件:
from apscheduler.schedulers.background import BackgroundScheduler
from django_apscheduler.jobstores import DjangoJobStore, register_events
from django.utils import timezone
from django_apscheduler.models import DjangoJobExecution
import sys
# This is the function you want to schedule - add as many as you want and then register them in the start() function below
def hello():
print("Hello")
def start():
scheduler = BackgroundScheduler()
scheduler.add_jobstore(DjangoJobStore(), "default")
# run this job every 10 seconds
scheduler.add_job(hello, 'interval', seconds=10, jobstore='default')
register_events(scheduler)
scheduler.start()
print("Scheduler started...", file=sys.stdout)我的Django应用程序在本地主机上运行良好。我只是尝试在终端中每隔10秒打印一次“hello”,但有时一次打印3到4个。为什么会这样呢?这只是一个基础模板,用来帮助理解apscheduler。
发布于 2020-06-24 08:05:43
发生这种情况的主要原因是,如果您在运行开发服务器时没有设置--noreload标志,这将导致调度器被调用两次(有时会更多)。
当您在开发中运行您的服务器时,尝试如下所示:
python manage.py runserver localhost:8000 --noreload看看会发生什么。如果它仍然在发生,可能是时间间隔太近了,所以当你的系统到达它的时候,另一个版本仍然在被调用(即使它是一个非常短的函数)。Django将所有挂起、过期和正在运行的作业存储在数据库中,因此它必须在每次交易后存储作业的记录。尝试扩大间隔,看看会发生什么。
如果这些都不起作用,请发布您正在使用的其余代码,我将使用其他选项更新我的答案。我在过去也遇到过类似的问题,但通过设置--noreload选项解决了这个问题。当您在生产环境中使用DEBUG=False在常规web服务器后面运行它时,它应该也会自动解析。
https://stackoverflow.com/questions/62544834
复制相似问题