Oct-27-2018, 10:59 PM
Well, the wav file is hard to kill. It only dies with CTR-C which I cannot use.
Your code returns the python pid and not the mplayer's.
This' the modified code:
Your code returns the python pid and not the mplayer's.
This' the modified code:
import multiprocessing import os import psutil import time from subprocess import check_output file = 'music.wav' def killtree(pid, including_parent=True): parent = psutil.Process(pid) for child in parent.children(recursive=True): print("child", child) child.kill() if including_parent: parent.kill() ## this program def song(): command = 'mplayer -softvol -volume 1 %s 1>/dev/null 2>&1' % file os.system(command) pid=os.getpid() ##this sketch's pid!! mp = multiprocessing.Process(target=song) mp.start() p= psutil.Process(mp.pid) print 'pid =', pid, p print "status", mp.is_alive() time.sleep(0.5) if mp.is_alive(): print("terminate") mp.terminate() mp.join() else: print "already terminated" print 'pid =', pid, p print "status", mp.is_alive() killtree(pid)