我试图杀死一个子进程,开始于:
playing_long = Popen(["omxplayer", "/music.mp3"], stdout=subprocess.PIPE)
过了一会儿
pid = playing_long.pid
playing_long.terminate()
os.kill(pid,0)
playing_long.kill()
这不起作用。这两种解决方案都没有指出
How to terminate a python subprocess launched with shell=True
注意到我使用的是线程,当您使用线程时不建议使用preexec_fn (或者至少这是我所读到的,反正它也不起作用)。
为什么它不起作用?代码中没有错误消息,但我必须手动终止-9进程,以停止侦听mp3文件。
谢谢
编辑:在here中,我在杀死()之后添加了wait()。令人惊讶的是,在重新启动进程之前,我会检查是否还在等待,这样我就不会用mp3文件开始合唱了。
EDIT2:问题是,omxplayer启动了第二个进程,而我并没有杀死它,它负责实际的音乐。
它打印'NoneType‘对象,没有属性’写‘。即使在启动popen进程之后立即使用此代码时,它仍然会失败。
playing_long = subprocess.Popen(["omxplayer", "/home/pi/Motion_sounds/music.mp3"], stdout=subprocess.PIPE)
time.sleep(5)
playing_long.stdin.write('q')
playing_long.stdin.flush()
EDIT3:当时的问题是,我没有在popen行中建立stdin行。现在是
playing_long = subprocess.Popen(["omxplayer", "/home/pi/Motion_sounds/music.mp3"], stdin=subprocess.PIPE, stdout=subprocess.PIPE)
time.sleep(5)
playing_long.stdin.write(b'q')
playing_long.stdin.flush()
*需要指定它是我用stdin编写的字节
发布于 2015-01-28 00:34:32
最后解决方案(见问题中编辑的过程):
playing_long = subprocess.Popen(["omxplayer", "/home/pi/Motion_sounds/music.mp3"], stdin=subprocess.PIPE, stdout=subprocess.PIPE)
time.sleep(5)
playing_long.stdin.write(b'q')
playing_long.stdin.flush()
https://stackoverflow.com/questions/28137161
复制