简体   繁体   English

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

[英]Raspberry Pi Model B Threading. Run 2 python scripts concurrently

I'm hoping you can help (as always). 希望您能提供帮助(一如既往)。 I'm making a photobooth and in short have a Python script to control the light (switches on for 30 seconds) and another to take 4 photos for the photobooth. 我正在制作一个照相亭,简而言之,有一个Python脚本来控制灯光(打开30秒),另一个为照相亭拍摄4张照片。 I need to run the light script followed by the camera script 5 seconds later. 5秒钟后,我需要先运行灯光脚本,再运行相机脚本。 I'm sure this is easy, but can't work it out. 我敢肯定这很容易,但是无法解决。 Here's what I've tried: 这是我尝试过的:

I'm not even sure if this is correct for the Raspberry Pi, but this is what I've tried inc. 我什至不确定这是否适用于Raspberry Pi,但这就是我尝试过的公司。 variations: 变化:

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

Any help you can provide would be great, as I'm sure im being an idiot. 您可以提供的任何帮助都会很棒,因为我确定我是个白痴。

as mentioned in the comments threading expects to run python code ... not a python file ... you can just use subprocess to accomplish that you want 如注释中所述,线程期望运行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()

the communicate call at the end just makes it block and wait for camtest.py to complete before exiting this script (as well as getting the output from the script) 最后的交流调用只会使其阻塞,并等待camtest.py完成后再退出此脚本(以及从脚本获取输出)

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

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