Python 调度程序中的异常
Exception in Python scheduler
我正在使用 Google Colab,在 Python 上还很新,但我正在尝试构建一个具有三种状态“绿色”、“黄色”和“红色”的机器人。这是基本的机器人,每 10 秒运行一次,如果它是 运行 并且我按下停止按钮一次,它会变成黄色,但是我第二次按下停止按钮时我得到“在处理上述过程中异常,发生另一个异常:”。这是代码:
import sched, time, datetime, json
try:
def run_bot(sc):
if botstate == "green":
print(datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") + " doing " + botstate + " state stuff")
s.enter(10, 1, run_bot, (sc,))
s.run()
elif botstate == "yellow":
print(datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") + " doing " + botstate + " state stuff")
s.enter(10, 1, run_bot, (sc,))
s.run()
else:
print(datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") + " FINISHED")
# START POINT
print(datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
botstate = "green"
s = sched.scheduler(time.time, time.sleep)
s.enter(10, 1, run_bot, (s,))
s.run()
except KeyboardInterrupt:
if botstate == "green":
botstate = "yellow"
print(datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") + " botstate set to " + botstate + ", shutdown state entered")
s.run()
else:
print(datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") + " HARD FINISHED")
非常感谢任何帮助!
第一次调用 s.run()
时,您处于 try
块中。当您按下 ctrl-c 时,您将进入 KeyboardInterrupt 异常块。
然后在该块中再次调用 s.run()
,但这次您不在 try
块中,因此再次按 ctrl-c 会引发未捕获的异常。
根据已接受的答案,我更改了异常部分,此示例现在可以完美运行:
import sched, time, datetime, json
try:
def run_bot(sc):
if botstate == "green":
print(datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") + " doing " + botstate + " state stuff")
elif botstate == "yellow":
print(datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") + " doing " + botstate + " state stuff")
# recurse
s.enter(10, 1, run_bot, (sc,))
s.run()
# START POINT
print (datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") + " STARTED")
botstate = "green"
s = sched.scheduler(time.time, time.sleep)
s.enter(10, 1, run_bot, (s,))
s.run()
except KeyboardInterrupt:
try:
if botstate == "green":
botstate = "yellow"
print(datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") + " botstate set to " + botstate + ", shutdown state entered")
s.run()
except KeyboardInterrupt:
print(datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") + " FINISHED")
我正在使用 Google Colab,在 Python 上还很新,但我正在尝试构建一个具有三种状态“绿色”、“黄色”和“红色”的机器人。这是基本的机器人,每 10 秒运行一次,如果它是 运行 并且我按下停止按钮一次,它会变成黄色,但是我第二次按下停止按钮时我得到“在处理上述过程中异常,发生另一个异常:”。这是代码:
import sched, time, datetime, json
try:
def run_bot(sc):
if botstate == "green":
print(datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") + " doing " + botstate + " state stuff")
s.enter(10, 1, run_bot, (sc,))
s.run()
elif botstate == "yellow":
print(datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") + " doing " + botstate + " state stuff")
s.enter(10, 1, run_bot, (sc,))
s.run()
else:
print(datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") + " FINISHED")
# START POINT
print(datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
botstate = "green"
s = sched.scheduler(time.time, time.sleep)
s.enter(10, 1, run_bot, (s,))
s.run()
except KeyboardInterrupt:
if botstate == "green":
botstate = "yellow"
print(datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") + " botstate set to " + botstate + ", shutdown state entered")
s.run()
else:
print(datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") + " HARD FINISHED")
非常感谢任何帮助!
第一次调用 s.run()
时,您处于 try
块中。当您按下 ctrl-c 时,您将进入 KeyboardInterrupt 异常块。
然后在该块中再次调用 s.run()
,但这次您不在 try
块中,因此再次按 ctrl-c 会引发未捕获的异常。
根据已接受的答案,我更改了异常部分,此示例现在可以完美运行:
import sched, time, datetime, json
try:
def run_bot(sc):
if botstate == "green":
print(datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") + " doing " + botstate + " state stuff")
elif botstate == "yellow":
print(datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") + " doing " + botstate + " state stuff")
# recurse
s.enter(10, 1, run_bot, (sc,))
s.run()
# START POINT
print (datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") + " STARTED")
botstate = "green"
s = sched.scheduler(time.time, time.sleep)
s.enter(10, 1, run_bot, (s,))
s.run()
except KeyboardInterrupt:
try:
if botstate == "green":
botstate = "yellow"
print(datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") + " botstate set to " + botstate + ", shutdown state entered")
s.run()
except KeyboardInterrupt:
print(datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") + " FINISHED")