[英]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.