结束过程和子进程

时间:2017-10-19 12:56:06

标签: go raspberry-pi

我正在尝试正确终止命令

a= np.array([np.array([1,2,3]),
             np.array([4,5,6]),
             np.array([7,8,9])])

print(a)

>>> array([[1, 2, 3],
           [4, 5, 6],
           [7, 8, 9]])

# get 2nd values
print(a[:, 1])

>>> array([2, 5, 8])


# get mean values
print(np.mean(a, axis=0))

>>>  array([ 4.,  5.,  6.])

我正在尝试杀死我的Raspberry-Pi上的当前c := exec.Command("omxplayer", "video.mp4") c.Start() // send kill signal to terminate command at later stage time.Sleep(4*time.Second) c.Process.Kill() c.Wait() // should wait for program to fully exit // start again with new video c := exec.Command("omxplayer", "video2.mp4") c.Start() 进程,以便我可以使用新视频重新启动它。

发送omxplayer信号后,我会在启动新命令之前调用Kill等待当前命令结束。

问题是第一个命令没有停止,但是下一个命令仍在启动。所以我最终会同时播放多个视频。

1 个答案:

答案 0 :(得分:1)

omxplayer正在分配一个新流程。特别是对于omxplayer,我可以将“q”字符串发送到其StdinPipe。这就像按下键盘上的q键,退出程序,结束父进程和子进程:

c := exec.Command("omxplayer", "video.mp4")
c.Start()
p := c.StdinPipe()
_, err := p.Write([]byte("q")) // send 'q' to the Stdin of the process to quit omxplayer

c.Wait() // should wait for program to fully exit

// start again with new video
c := exec.Command("omxplayer", "video2.mp4")
c.Start()

另一个更通用的选择是创建一个包含父进程和子进程的进程组并终止:

c := exec.Command("omxplayer", "video.mp4")
// make the process group id the same as the pid
c.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
c.Start()

// sending a negative number as the PID sends the kill signal to the group
syscall.Kill(-c.Process.Pid, syscall.SIGKILL)

c.Wait()

基于this Medium post