Python 调度程序 - 即时调整时间间隔

Python Scheduler - Adjust time interval on-the-fly

我正在使用 Python Advanced Scheduler 库,我希望即时更改时间间隔,但这似乎不适用于我的实现。我正在做的是根据当前 model_retrain_freq 经理成员值设置时间间隔,然后使用 public setter 稍后更改该间隔。以后的时间永远不会被拾起。建议?请参考以下内容:

run.py

from apscheduler.schedulers.background import BackgroundScheduler
import atexit

from app.main.manager import Manager

manager = Manager()

sched = BackgroundScheduler(daemon=True)
sched.add_job(
    manager.model_expiry_callback,
    "interval",
    seconds=manager.model_retrain_freq,
)
sched.start()
atexit.register(lambda: sched.shutdown(wait=False))

# testing
import time

time.sleep(10)
manager.model_retrain_freq = 1

time.sleep(9000)

manager.py

class Manager:
 def __init__(self):
        self._model_retrain_freq = 5

@property
def model_retrain_freq(self):
    """Get model retrain frequency"""
    return self._model_retrain_freq

@model_retrain_freq.setter
def model_retrain_freq(self, value):
    """Set model retrain frequency"""
    self._model_retrain_freq = value

更改 _model_retrain_freq 的值不会影响创建的作业。更改 _model_retrain_freq.

后,您需要重新安排作业

存储创建的作业。

job = sched.add_job(
    manager.model_expiry_callback,
    "interval",
    seconds=manager.model_retrain_freq,
)

更改_model_retrain_freq的值后需要重新安排时:

job.reschedule(trigger="interval", seconds=manager.model_retrain_freq)