简体   繁体   English

流程未终止

[英]Process not terminating

I am trying to run all mp3 files in the background by creating a process using the multiprocessing library. 我正在尝试通过使用多处理库创建一个进程在后台运行所有mp3文件。

import os
import subprocess
from multiprocessing import Process

def music_player():

    music_folder = "/home/pi/Music/"
    files = os.listdir(music_folder)
    for mp3_file in files:
        print("playing " + mp3_file)
        p = subprocess.Popen(["omxplayer","-o","local",music_folder+mp3_file],
                          stdout = subprocess.PIPE,
                          stdin = subprocess.PIPE,
                          stderr = subprocess.PIPE)
        print(p)
        print(p.poll())
        print(p.pid)
        p.wait()



p = Process(target = music_player)
print(p, p.is_alive())
p.start()
print(p.pid)
print(p, p.is_alive())
command = raw_input()
if(command == "stop"):
    print("terminating...")
    p.terminate()
    print(p, p.is_alive())
    print(p.exitcode)  

After entering the "stop" command the code exits but the music is still running and on executing ps I see 2 process of omxplayer which I then have to manually kill through kill <pid> to make the music stop. 输入“停止”命令后,代码退出,但音乐仍在运行,并且在执行ps我看到omxplayer的2个进程,然后必须通过kill <pid>手动将其停止,以使音乐停止。

I previously tried using the subprocess library and killing the process using kill() and terminate() but the same issue occurred. 我以前尝试使用子进程库,并使用kill() and terminate()进程kill() and terminate()但是发生了相同的问题。

First observation, you don't need the multiprocessing module for what you're doing here. 首先观察,您不需要在这里执行多处理模块。 subprocess is for creating and managing processes which will run other scripts and programs; 子流程用于创建和管理将运行其他脚本和程序的流程; multiprocessing is for creating and managing processes which will be calling code which is already internal to your (parent) script. 多重处理用于创建和管理将调用您(父)脚本内部的代码的进程。

I suspect that your seeing the effect of buffering. 我怀疑您看到缓冲的效果。 By the time you kill this process it's already buffered a significant amount of music out to the hardware (or even the OS buffers for the device). 在您终止此过程时,它已经将大量音乐缓冲到硬件(甚至设备的OS缓冲区)中。

What happens if you start the same program omxplayer from your shell, but in the background (as the & token to the end of your Unix shell command line to push a program into the background). 如果从外壳启动同一程序omxplayer ,但在后台启动(会在Unix Shell命令行末尾以标记将程序推入后台),会发生什么情况。 Then use the kill command on that process and see if you see the same results. 然后在该进程上使用kill命令,看看是否看到相同的结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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