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
tommark
V2EX  ›  Python

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

  •  
  •   tommark · 2013-10-24 22:34:42 +08:00 · 9645 次点击
    这是一个创建于 3830 天前的主题,其中的信息可能已经有所发展或是发生改变。
    写了一个Python代码,用subprocess调用外部exe程序,这个程序会监控文件,然后给出相关的输出,问题是,在subprocess用管道重定向后,exe里的输出是缓存在,只有输出一定数据量时才会传到管道中,如何让subprocess里的管道无缓存运行? 虽然subprocess的Popen里有个buffersize项,但是设成0和1都没用,不知有没有谁遇到过这个问题,求解!!!!!
    9 条回复    1970-01-01 08:00:00 +08:00
    dreampuf
        1
    dreampuf  
       2013-10-25 02:23:07 +08:00
    不想自己维护buf定期输出就用python-sh模块
    stevenyou
        2
    stevenyou  
       2013-10-25 07:25:45 +08:00
    clouder
        3
    clouder  
       2013-10-25 08:48:05 +08:00
    fd=os.poopen(cmd)
    print fd.read()
    fd.close()
    lixm
        4
    lixm  
       2013-10-25 10:20:25 +08:00
    non-block IO就可以了, 可以参考这个 https://gist.github.com/7148531
    emptyhua
        5
    emptyhua  
       2013-10-25 17:04:19 +08:00
    xierch
        6
    xierch  
       2013-10-25 18:38:07 +08:00
    这是得在 exe 里调用 flush 吧..?
    tommark
        7
    tommark  
    OP
       2013-10-25 22:39:05 +08:00
    @xierch 不能在exe里flush,这个是用来测试的,没法修改exe的代码。
    tommark
        8
    tommark  
    OP
       2013-10-25 22:42:11 +08:00
    @emptyhua 这个我试过,但是不行。
    #model.py
    while True:
    print('enter:')
    s = input()
    print(s)
    if s == 'q':break
    用model.py做为测试exe,主程序调用它,ping.stdout.readline会被阻塞的。
    wynemo
        9
    wynemo  
       2013-10-26 21:41:58 +08:00
    其实stdout=sys.stdout 应该就可以了吧。。。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1134 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 18:25 · PVG 02:25 · LAX 11:25 · JFK 14:25
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.