Raspberry Pi B型线程。同时运行2个Python脚本

Raspberry Pi B型线程。同时运行2个Python脚本

问题描述:

我希望你能帮助(一如既往)。我正在制作一个PhotoBooth,简而言之,它有一个Python脚本来控制灯光(开启30秒),另一个灯泡为PhotoBooth拍摄4张照片。 我需要在5秒钟之后运行灯光脚本,然后运行摄像头脚本。我确信这很容易,但无法解决。以下是我所尝试的:Raspberry Pi B型线程。同时运行2个Python脚本

我甚至不确定这是否适用于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() 

任何帮助你可以提供将是伟大的,因为我敢肯定我是一个白痴。

+0

'Thread'目标参数应该是可调用的对象,而不是字符串。 –

+0

我希望我能像你做Lukasz一样了解这些知识。我将如何做到这一点 - 我是一个相对n00b – user1721451

+0

为什么你需要用sudo运行脚本? –

如在评论中提到穿线预计运行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完成(以及从脚本获取输出)

+0

非常感谢Joran,这是我无法理解的。 – user1721451