python3 subprocess.Popen 为什么会创建两个进程呢?

2018-07-20 01:00:19 +08:00
 tunnyios

开发环境:ubuntu14.0.4

python 版本: 3.6 执行如下方法

cmd = 'ffmpeg -i "http://60.171.135.231:86/pag/60.171xxxxxxxxxxxxxxxx" -f hls -vcodec copy -y /var/lib/xxx.m3u8 '
ps = subprocess.Popen(cmd, stdin=None, stdout=None, stderr=subprocess.PIPE, shell=True)

会创建两个进程,如下。这是为什么呢? subprocess.Popen 不是只会创建一个进程吗?

root@VM-46047ef0-5d57-439c-b357-2f8e6f0be418:/var/lib/docker/rtsp2m3u8/qianshan01# ps aux | grep ffmpeg 
root     112225  0.0  0.0   4456   760 pts/4    S+   00:30   0:00 /bin/sh -c ffmpeg -i "http://60.171.135.231:86/pag/60.171.135.231/7302/001175/0/SUB/TCP/live.m3u8" -fflags flush_packets -max_delay 2 -reset_timestamps 1 -flags -global_header -hls_time 2 -hls_list_size 10 -f hls -hls_flags delete_segments -use_localtime 1 -vcodec copy -y /var/lib/docker/rtsp2m3u8/qianshan01/qianshan01.m3u8
root     112227  0.0  0.0  91928 13348 pts/4    S+   00:30   0:00 ffmpeg -i http://60.171.135.231:86/pag/60.171.135.231/7302/001175/0/SUB/TCP/live.m3u8 -fflags flush_packets -max_delay 2 -reset_timestamps 1 -flags -global_header -hls_time 2 -hls_list_size 10 -f hls -hls_flags delete_segments -use_localtime 1 -vcodec copy -y /var/lib/docker/rtsp2m3u8/qianshan01/qianshan01.m3u8
root     112291  0.0  0.0  12156  2216 pts/2    R+   00:30   0:00 grep --color=auto ffmpeg 

在执行ps.kill()或者ps.terminate()方法时,只能杀死进程 112225,进程 112227 仍然存在,这该如何解决呢?

尝试过,在subprocess.Popen()中增加,preexec_fn=os.setsid参数,但是在之后执行ps.kill()或者ps.terminate()方法时,依然只能杀死第一个进程。

2898 次点击
所在节点    Python
4 条回复
weyou
2018-07-20 01:16:00 +08:00
shell=False 试试
xiaket
2018-07-20 06:39:18 +08:00
`shell=True`就是`sh -c`啊.
xiaket
2018-07-20 06:39:41 +08:00
顺便说下 14.04 该升级了吧...
tunnyios
2018-07-20 16:58:56 +08:00
谢谢各位了,已经找到问题了。一开始用的 mac 环境,使用 shell=True 参数,subprocess.Popen()只会产生一个进程。部署到线上 linux 服务器上后,就一直是两个进程。
现已解决:
修改 shell=False, 并且 args 参数使用列表的形式,将命令拼接起来,执行就好了。

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/472518

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX