【问题标题】:Subprocess always automatically starts when running python script运行python脚本时子进程总是自动启动
【发布时间】:2020-02-23 01:25:04
【问题描述】:

我正在尝试制作一个 python 脚本,该脚本在打开开关 (LOW) 时启动 mdk3,并在关闭 (HIGH) 时将其杀死。但是,无论开关的位置如何,mdk3 命令总是在脚本启动时启动。启动脚本时将开关置于 ON 位置,然后在运行时将其关闭,这会按预期杀死命令。但是,当它重新打开时,它不会再次开始运行。有趣的是,设置为打印的文本完全符合预期。我的代码如下:

import RPi.GPIO as GPIO
import time
import subprocess
import os
import signal

FSU = 'sudo mdk3 mon0 d'

pro = 'subprocess.Popen(FSU, stdout=subprocess.PIPE, shell=True, preexec_fn=os.setsid)'



# tell the GPIO module that we want to use the chip's numbering scheme
GPIO.setmode(GPIO.BCM)

# Set up GPIO16 as an input with internal pull-up resistor to hold it HIGH until it is pulled down to GND by the connected switch
GPIO.setup(16, GPIO.IN, pull_up_down=GPIO.PUD_UP)

running = False

while True:
    if GPIO.input(16) == GPIO.LOW:
        if running == False:
            print('Button was pushed!')
            pro
            running = True
            time.sleep(0.1)
        elif running == True:
            print('The process is running.')
            time.sleep(0.1)
    elif GPIO.input(16) == GPIO.HIGH and running == True:
        os.killpg(os.getpgid(pro.pid), signal.SIGTERM)
        print('Process killed.')
        running = False
        time.sleep(0.1)
    elif running == False:
        print('The process is not running.')
        time.sleep(0.1)
    else:
        print('Critical error.')
time.sleep(0.1)

我使用自己的循环来轮询 GPIO 引脚而不是 RPi.GPIO 库中内置的事件检测的原因是因为它除了问题之外什么都没给我带来,而且我自己做这件事似乎更简单。任何帮助将不胜感激。

编辑:我不知道为什么我没有想到把这个电话放在引号里。现在它不在脚本启动时运行,但它只运行一次。如:我在开关关闭的情况下启动脚本,但它没有运行(如预期的那样)。我将其打开并运行。我把它关掉,它成功地杀死了脚本。但是,将其重新打开不会重新启动脚本。抱歉,如果这是一个不好的解释。

【问题讨论】:

  • mks 始终由subprocess.Popen() 调用在脚本开头启动。
  • 我不知道为什么我没有想到把这个电话放在引号里。现在它不在脚本启动时运行,但它只运行一次。如:我在开关关闭的情况下启动脚本,但它没有运行(如预期的那样)。我将其打开并运行。我把它关掉,它成功地杀死了脚本。但是,将其重新打开不会重新启动脚本。

标签: python python-3.x bash raspberry-pi subprocess


【解决方案1】:

subprocess.Popen() 一被调用就启动进程并返回进程。
因此,您可以在需要运行时通过再次调用相同的函数来简单地在循环中再次启动该进程。

稍微修改你的代码:

import RPi.GPIO as GPIO
import time
import subprocess
import os
import signal

proc = subprocess.Popen(FSU, stdout=subprocess.PIPE, shell=True, preexec_fn=os.setsid)

FSU = 'sudo mdk3 mon0 d'

# tell the GPIO module that we want to use the chip's numbering scheme
GPIO.setmode(GPIO.BCM)

# Set up GPIO16 as an input with internal pull-up resistor to hold it HIGH until it is pulled down to GND by the connected switch
GPIO.setup(16, GPIO.IN, pull_up_down=GPIO.PUD_UP)

running = False

while True:
    if GPIO.input(16) == GPIO.LOW:
        if running == False:
            print('Button was pushed!')
            # declare the proc variabe again and also start the process
            proc = subprocess.Popen(FSU, stdout=subprocess.PIPE, shell=True, preexec_fn=os.setsid)
            running = True
            time.sleep(0.1)
        elif running == True:
            print('The process is running.')
            time.sleep(0.1)
    elif GPIO.input(16) == GPIO.HIGH and running == True:
        os.killpg(os.getpgid(pro.pid), signal.SIGTERM)
        print('Process killed.')
        running = False
        time.sleep(0.1)
    elif running == False:
        print('The process is not running.')
        time.sleep(0.1)
    else:
        print('Critical error.')
time.sleep(0.1)

More on subprocess

【讨论】:

  • 你在我输入评论回复时回答了,哈!不过,这很好用,谢谢。
  • proc 的第一个定义未使用、不需要,并且可能会造成混淆。
  • martineau,第一个定义proc的原因是默认启动进程,如果不需要也终止它,如果没有定义可能会导致未定义错误。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-11-07
  • 2020-02-04
  • 1970-01-01
  • 2018-12-04
  • 1970-01-01
  • 2018-08-29
  • 2013-02-16
相关资源
最近更新 更多