Raspberry Pi 模型 B 线程。 运行 2 python 个脚本并发

Raspberry Pi Model B Threading. Run 2 python scripts concurrently

我希望你能提供帮助(一如既往)。我正在制作一个照相亭,简而言之,有一个 Python 脚本来控制灯光(打开 30 秒),另一个脚本可以为照相亭拍摄 4 张照片。 我需要 运行 灯光脚本,然后是 5 秒后的相机脚本。我敢肯定这很容易,但无法解决。这是我尝试过的:

我什至不确定这对 Raspberry Pi 是否正确,但这是我尝试过的方法。变化:

import threading
import time
def light():
    while time.time() <= start_time:
        pass
    threading.Thread(target="sudo python /home/pi/python/light30.py").start()
def camera():
    while time.time() <= start_time:
        pass
    threading.Thread(target="sudo python /home/pi/python/camtest.py").start()
start_time=time.time()+5
threading.Thread(target=light).start()
threading.Thread(target=camera).start()

如果您能提供任何帮助,那将是非常好的,因为我确定我是个白痴。

正如评论中提到的,threading 期望 运行 python 代码......不是 python 文件......你可以只使用子进程来完成你想要的

import subprocess
import time

lights_proc = subprocess.Popen(["/usr/bin/python","/home/pi/python/light30.py"])
time.sleep(5) # sleep for 5 seconds
print subprocess.Popen(["/usr/bin/python","/home/pi/python/camtest.py"],stdout=subprocess.PIPE,stderr=subprocess.PIPE).communicate()

最后的通信调用只是让它阻塞并等待 camtest.py 完成,然后退出此脚本(以及从脚本获取输出)