简体   繁体   中英

capturing and processing output from process

I want to make wrapper in python that will capture output from another python program and later send that output to multiple places like logger and to telnet server but i am stuck at spawning these processes and it's communication
i have already tried using subprocess for running and capturing but i had problem it returned output only when process exited that is not acceptable for infinite loops that most programs are at basic so for example i have these 2 scripts

i = 0
running = True
while running:
    print(i)
    sleep(10) #simulate program idling
    if i == 20:
        running = False
    else:
        i += 1

print("Exit reached")

and second one should be something like this

program = "first_script.py"
program.start() #non blocking?

while program.exist:
    print(program.get_output())
    sleep(5)

so because it will ask for output every 5 seconds it should get 2 times the same output but also there i am not sure if i shoud think about getting some event listen function that will add every next line to the buffer or something like example above that should receive every time when asked whole output from some internal buffer
My question is what approach should i use and how to use while capturing this output and sending it to another process

You can do it something like this, which uses subprocess and at least works on Windows in my testing (and I think it would work on other OSs as well):

import subprocess
import sys

# Execute another Python scipt as a separate process and echo its output.
kwargs = dict(stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
              universal_newlines=True)
args = [sys.executable, 'second_script.py']
with subprocess.Popen(args, **kwargs).stdout as output:
    for line in output:
        print(line, end='')  # Process the output...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM