gpt4 book ai didi

python - 为什么这不起作用?这是 apscheduler 错误吗?

转载 作者:行者123 更新时间:2023-11-30 22:48:49 27 4
gpt4 key购买 nike

当我运行这个程序时,它会等待一分钟,然后打印“灯打开”,然后等待两分钟,然后打印“灯关闭”。之后,apscheduler 似乎发疯了,并很快在两者之间快速交替。

我是不是偶然发现了 apscheduler 错误,或者为什么会发生这种情况?

from datetime import datetime, timedelta
import time
import os, signal, logging
logging.basicConfig(level=logging.DEBUG)

from apscheduler.schedulers.background import BackgroundScheduler
scheduler = BackgroundScheduler()


def turn_on():
#Turn ON
print('##############################Lights on')


def turn_off():
#Turn off
print('#############################Lights off')


def schedule():
print('Lights will turn on at'.format(lights_on_time))
if __name__ == '__main__':

while True:
lights_on_time = (str(datetime.now() + timedelta(minutes=1)))
lights_off_time = (str(datetime.now() + timedelta(minutes=2)))

scheduler.add_job(turn_on, 'date', run_date=lights_on_time)
scheduler.add_job(turn_off, 'date', run_date=lights_off_time)
try:
scheduler.start()
signal.pause()
except:
pass

print('Press Ctrl+{0} to exit'.format('Break' if os.name == 'nt' else 'C'))

try:
# This is here to simulate application activity (which keeps the main thread alive).
while True:
time.sleep(2)
except (KeyboardInterrupt, SystemExit):
# Not strictly necessary if daemonic mode is enabled but should be done if possible
scheduler.shutdown()

最佳答案

您正在用事件淹没调度程序。您正在使用BackgroundScheduler,这意味着scheduler.start() 正在退出并且不等待事件发生。最简单的修复可能是不使用BackgroundScheduler(使用BlockingScheduler),或者在循环中放置一个sleep(180)。

关于python - 为什么这不起作用?这是 apscheduler 错误吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40046700/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com