V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
tunnyios
V2EX  ›  Python

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

  •  
  •   tunnyios ·
    chenzao1024 · 2018-07-20 01:00:19 +08:00 · 2869 次点击
    这是一个创建于 2079 天前的主题,其中的信息可能已经有所发展或是发生改变。

    开发环境: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()方法时,依然只能杀死第一个进程。

    4 条回复    2018-07-20 16:58:56 +08:00
    weyou
        1
    weyou  
       2018-07-20 01:16:00 +08:00 via Android
    shell=False 试试
    xiaket
        2
    xiaket  
       2018-07-20 06:39:18 +08:00
    `shell=True`就是`sh -c`啊.
    xiaket
        3
    xiaket  
       2018-07-20 06:39:41 +08:00
    顺便说下 14.04 该升级了吧...
    tunnyios
        4
    tunnyios  
    OP
       2018-07-20 16:58:56 +08:00
    谢谢各位了,已经找到问题了。一开始用的 mac 环境,使用 shell=True 参数,subprocess.Popen()只会产生一个进程。部署到线上 linux 服务器上后,就一直是两个进程。
    现已解决:
    修改 shell=False, 并且 args 参数使用列表的形式,将命令拼接起来,执行就好了。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1036 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 22:29 · PVG 06:29 · LAX 15:29 · JFK 18:29
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.