开发者

View output from long running external process called from Python

开发者 https://www.devze.com 2023-04-05 20:01 出处:网络
I have a very long running process that I would like to call from a Python program.This process outputs a lot of i开发者_JAVA百科nformation to stdout.I would like to see the output from my called prog

I have a very long running process that I would like to call from a Python program. This process outputs a lot of i开发者_JAVA百科nformation to stdout. I would like to see the output from my called program on the command line as it is running. I have read about Popen, and tried

p = Popen(cmd, stdout=PIPE, stderr=PIPE)
stdout, stderr = p.communicate()

and variants of this, but the output from cmd doesn't get displayed until cmd is finished running.

How do I view the output of cmd while cmd is running?


I have figured this out by using part of what was mentioned in the comments, and combined that with the following

# read line without blocking
while not p.poll():
    try: 
        line = q.get_nowait() # or q.get(timeout=.1)
    except Empty:
        pass # Do nothing
    else: # got line
        print line
0

精彩评论

暂无评论...
验证码 换一张
取 消