Python subprocess output on windows?

前端 未结 2 1926
攒了一身酷
攒了一身酷 2021-01-02 22:25

I\'m running into some difficulties getting output from a subprocess stdout pipe. I\'m launching some third party code via it, in order to extract log output. Up until a r

相关标签:
2条回答
  • 2021-01-02 23:00

    First I would recommend that you simplify this example to make sure you can actually read anything. Remove the complication of the thread from the mix:

    proc = subprocess.Popen("third_party.exe", stdout=subprocess.PIPE, bufsize=1)
    print proc.communicate()
    

    If that works, great. Then you are having problems possibly with how you are reading the stdout directly or possibly in your thread.

    If this does not work, have you tried piping stderr to stdout as well?

    proc = subprocess.Popen("third_party.exe", 
                            stdout=subprocess.PIPE, 
                            stderr=subprocess.STDOUT, bufsize=1)
    

    Update

    Since you say communicate() is deadlocking, here is another approach you can try to see if its a problem with the internal buffer of subprocess...

    import tempfile
    import subprocess
    
    w = tempfile.NamedTemporaryFile()
    p = subprocess.Popen('third_party.exe', shell=True, stdout=w, 
                            stderr=subprocess.STDOUT, bufsize=0)
    
    with open(w.name, 'r') as r:
        for line in r:
            print line
    w.close()
    
    0 讨论(0)
  • 2021-01-02 23:01
    args = ['svn','log','-v']
    
    def foo(info=''):
        import logging
        import subprocess
        import tempfile
        try:
            pipe = subprocess.Popen(args,bufsize = 0,\
                stdout = subprocess.PIPE,\
                stderr=subprocess.STDOUT)
        except Exception as e:
            logging.error(str(e))
            return False
        while 1:
            s = pipe.stdout.read()
            if s:
                print s,
            if pipe.returncode is None:
                pipe.poll()
            else:
                break
        if not 0 == pipe.returncode:
            return False
        return True
    
    print foo()
    

    This one should works,not thread,temp file magic.

    0 讨论(0)
提交回复
热议问题