Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to kill a pid [SOLVED]
#7
Thanks for introducing me to multiprossesing. I finally got it working. Not sure if it's the best way of doing it but here's my final code.

import RPi.GPIO as GPIO
import time, os, signal, playsong
from multiprocessing import Process
from subprocess import check_output

buttonpin = 23 #GPIO23

GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup(buttonpin, GPIO.IN, pull_up_down=GPIO.PUD_UP)

m_pid = 0

def play():
    '''play music from playsong.py '''
    playsong.song()

def get_pid():
    '''gets mplayer's actual pid'''
    global m_pid
    m_pid = int(check_output(["pidof","-s","mplayer"]))
    print "mplayer's pid: ", m_pid

def kill_pid(pid):
    '''used to kill mplayer's pid'''
    os.kill(pid, signal.SIGKILL)

def button():
    '''check if button is pressed'''
    while True:
        input_state = GPIO.input(buttonpin)
        if input_state == False:
            print 'on'
            time.sleep(0.3) #debounce
            kill_pid(m_pid)

if __name__ == '__main__':
  p1 = Process(target=play)
  p1.start()
  p2 = Process(target=button)
  p2.start()
  p3 = Process(target=get_pid)
  p3.start()
  p1.join()
  p2.join()
  p3.join()
Reply


Messages In This Thread
How to kill a pid [SOLVED] - by ebolisa - Oct-26-2018, 09:36 PM
RE: How to kill a pid - by woooee - Oct-26-2018, 10:17 PM
RE: How to kill a pid - by ebolisa - Oct-27-2018, 09:14 AM
RE: How to kill a pid - by woooee - Oct-27-2018, 08:04 PM
RE: How to kill a pid - by ebolisa - Oct-27-2018, 10:59 PM
RE: How to kill a pid - by woooee - Oct-27-2018, 11:18 PM
RE: How to kill a pid [SOLVED] - by ebolisa - Oct-28-2018, 01:12 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Cannot run kill -9 through pyvomi on ESXi host nicktx 1 955 Jul-08-2024, 04:07 PM
Last Post: nicktx
  kill python execution program lebossejames 0 785 Mar-16-2024, 11:16 AM
Last Post: lebossejames
  How to immediately kill and restart a thread while using a time.sleep() inside it? philipbergwerf 4 5,230 Feb-07-2022, 04:16 PM
Last Post: Gribouillis
  Using a button to kill and restart a script duckredbeard 3 4,689 Sep-01-2020, 12:53 AM
Last Post: duckredbeard
  how to check for thread kill flag nanok66 1 2,918 May-09-2020, 10:06 PM
Last Post: nanok66
  kill thread or process asap, even during time.sleep nanok66 4 4,292 Apr-29-2020, 10:13 AM
Last Post: nanok66
  Linux: Automatically kill process with fixed exe path anddontyoucomebacknomore 4 4,230 Apr-22-2019, 07:35 AM
Last Post: anddontyoucomebacknomore

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020