推荐学习书目
› 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
tommark
V2EX  ›  Python

python用subprocess调用外部exe如何实时捕捉stdout输出

  •  
  •   tommark · Oct 24, 2013 · 10916 views
    This topic created in 4762 days ago, the information mentioned may be changed or developed.
    写了一个Python代码,用subprocess调用外部exe程序,这个程序会监控文件,然后给出相关的输出,问题是,在subprocess用管道重定向后,exe里的输出是缓存在,只有输出一定数据量时才会传到管道中,如何让subprocess里的管道无缓存运行? 虽然subprocess的Popen里有个buffersize项,但是设成0和1都没用,不知有没有谁遇到过这个问题,求解!!!!!
    9 replies  •  1970-01-01 08:00:00 +08:00
    dreampuf
    dreampuf
    Oct 25, 2013
    1
    不想自己维护buf定期输出就用python-sh模块
    stevenyou
    stevenyou
    Oct 25, 2013
    2
    clouder
    clouder
    Oct 25, 2013
    3
    fd=os.poopen(cmd)
    print fd.read()
    fd.close()
    lixm
    lixm
    Oct 25, 2013
    4
    non-block IO就可以了, 可以参考这个 https://gist.github.com/7148531
    xierch
    xierch
    Oct 25, 2013
    6
    这是得在 exe 里调用 flush 吧..?
    tommark
    tommark
    OP
    Oct 25, 2013
    7
    @xierch 不能在exe里flush,这个是用来测试的,没法修改exe的代码。
    tommark
    tommark
    OP
    Oct 25, 2013
    8
    @emptyhua 这个我试过,但是不行。
    #model.py
    while True:
    print('enter:')
    s = input()
    print(s)
    if s == 'q':break
    用model.py做为测试exe,主程序调用它,ping.stdout.readline会被阻塞的。
    wynemo
    wynemo
    Oct 26, 2013
    9
    其实stdout=sys.stdout 应该就可以了吧。。。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   1435 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 38ms · UTC 04:48 · PVG 12:48 · LAX 20:48 · JFK 23:48
    ♥ Do have faith in what you're doing.