如何在特定时间暂停或停止 python 程序(不基于经过的时间!)

How to pause or stop python program between certain hours (not based on elapsed time!)

我正在尝试找到一种方法让我的 python 程序持续 运行,但要确保主脚本仅在夜间 'active'(例如 [=28 之间) =] 和 04:30 例如)

背景:我的程序录制动作激活视频并激活泛光灯。

主要程序段如下:

...我现在 运行 每天晚上都这样做,然后在早上用 Ctrl-C 杀人..!

try:

signal.signal(signal.SIGHUP,SigHandler) # proper exit for SIGHUP (terminal hangups)

while GPIO.input(PIR_PIN)==1: # Loop until PIR output is stabilised at 0
    pass

GPIO.add_event_detect(PIR_PIN, GPIO.RISING, callback=MotionDetected)

signal.pause() #pauses main program forever & waits for PIR Rising Edge unless interrupted

except KeyboardInterrupt:
    print("")

finally:
    GPIO.output(16, GPIO.LOW) #floodlight off
    GPIO.cleanup()

有没有办法在并行线程中定期检查时间,如果时间在特定时间之间则暂停程序?

如果我在 try 语句之前添加线程语句,下面发布的解决方案是否适用于我的程序?...

首先,创建一个 cron 作业(我相信你在 Linux 因为 GPIO 调用),每天晚上在 21:30 启动脚本。

30 21 * * * python /path/to/python/script

然后,在 4:30 创建第二个 cron 作业,以终止 运行 您的程序中的所有现有进程。

30 4 * * * pkill -f /path/to/python/script # Will need to be edited to the actual process name that runs.

Killing a process by matching name

Python暂停https://github.com/jgillick/python-pause/支持暂停到某个日期时间。

另外 Java Threads 第 2 版有一个作业调度程序的示例代码;也许您可以将其用作蓝图。