python3 subprocess.Popen 为什么会创建两个进程呢? - V2EX
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 3390 次点击
    这是一个创建于 2644 天前的主题,其中的信息可能已经有所发展或是发生改变。

    开发环境: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`啊.
    /div>
    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     Solana     1075 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 27ms UTC 23:06 PVG 07:06 LAX 16:06 JFK 19:06
    Do have faith in what you're doing.
    ubao snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86