繁体   English   中英

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

[英]How run 2 script python in same time?

我解释了我的需要:我希望使用 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")

暂无
暂无

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

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