繁体   English   中英

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()

您可以提供的任何帮助都会很棒,因为我确定我是个白痴。

如注释中所述,线程期望运行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完成后再退出此脚本(以及从脚本获取输出)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM