gpt4 book ai didi

python - 如何同时运行 2 个脚本 python?

转载 作者:行者123 更新时间:2023-12-04 23:21:01 26 4
gpt4 key购买 nike

我解释了我的需求:我希望使用 python 脚本运行 ffmpeg(没关系),但我需要知道脚本是通过连接在我的 RPI 的 GPIO 上的闪烁 LED 启动的,但我不知道为什么我可以启动我的脚本并开始闪烁(或只是点亮)
你能帮我吗 ?请给我看灯;)

import RPi.GPIO as GPIO
import time
import os

GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup(4, GPIO.OUT)
GPIO.setup(22,GPIO.IN)

# fonction qui fait clignoter la led
def blink(led):
GPIO.output(led,True)
time.sleep(0.5)
GPIO.output(led,False)
time.sleep(1)

# input of the switch will change the state of the LED
while 1:
if ( GPIO.input(22) == True ):
print "start broadcast"
os.system("sudo /home/pi/videopi/webcam.sh")
blink(4) << not ok like this !
time.sleep(1)

最佳答案

假设您的 os脚本运行成功(我会推荐 subprocess),您所描述的称为并发 - https://realpython.com/python-concurrency/
我会像这样构造你的代码:

import RPi.GPIO as GPIO
import time
import os
from threading import Thread

GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup(4, GPIO.OUT)
GPIO.setup(22,GPIO.IN)

# fonction qui fait clignoter la led
def blink(led):
while True:
GPIO.output(led,True)
time.sleep(0.5)
GPIO.output(led,False)
time.sleep(1)

# input of the switch will change the state of the LED
while True:
if ( GPIO.input(22) == True ):
blinking = Thread(target=blink, args=(4,)) # create thread
blinking.start() # start blinking
print("start broadcast")
os.system("sudo /home/pi/videopi/webcam.sh")
blinking.join() # stops blinking once webcam.sh completed
print("broadcast complete")

关于python - 如何同时运行 2 个脚本 python?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66400950/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com