简体   繁体   中英

Subprocess always automatically starts when running python script

I'm trying to make a python script that starts mdk3 when a switch is flipped on (LOW) and kills it when it's switched off (HIGH). However, the mdk3 command always starts when the script is started, regardless of the switch's position. Starting the script with the switch in the ON position and then switching it OFF while it's running kills the command, as expected. However, it does not begin running again when switching it back ON. Interestingly, the text set to print functions exactly as would be expected. My code is as follows:

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)

The reason I'm using my own loop to poll the GPIO pins instead of the event detection built-in in the RPi.GPIO library is because it has caused me nothing but problems and doing it myself seemed simpler. Any help would be appreciated.

Edit: I'm not sure why I didn't think of putting that call into quotes. Now it doesn't run on script start, but it only runs once. As in: I start the script with the switch OFF, and it doesn't run (as expected). I switch it ON and it runs. I switch it back OFF and it successfully kills the script. However, switching it back ON doesn't restart the script. Sorry if this is a bad explanation.

subprocess.Popen() starts the process as soon as it is called and also returns the process.
So, you can simply start the process again in the loop when it needs to be running by calling the same function again.

Slightly modifying your code:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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