简体   繁体   中英

Runs two different commands from subprocess and capture the output of both the process Python

I am trying to run two different commands using subprocess module of python and capture the output from them and print it to the console...

XY_thread = Thread(target = run_command_XY)
TEXT_thread = Thread(target = run_command_text)

XY_thread.start()
TEXT_thread.start()

so, i get output from the XY_thread and not from the TEXT_thread. When i interchange the threads and start TEXT_thread first, then only TEXT_thread output is being displayed..

Im struck here, please help. I'm i missing something here?

import subprocess
from subprocess import Popen, PIPE
from threading import Thread


def run_command_text():
    command = "SOME COMMAND"

    process_ = Popen(command.split(), stdout = PIPE, shell = False)

    prev_line = None
    retcode = process_.poll()

    while(process_.poll() == None):
        line_ = process_.stdout.readline().decode('utf-8')

        if "SOME CONDITION" in line_:
        # does some operation and fetches the text(no issues with this part)
            if text != "":
                print(text)         
                prev_line = line_

        if retcode is not None:
            break

def run_command_XY():
    command = "SOME COMMAND"


    process = Popen(command.split(), stdout = PIPE,  shell = False)
    retcode = process.poll()

    while(process.poll() == None):

        line = process.stdout.readline().decode('utf-8')
    # does some operation and fetches the X and Y(no issues with this part)
        print(X+"  "+Y)

        if retcode is not None:
            break

if __name__ == '__main__':

    # multiprocessor = list()

    XY_thread = Thread(target = run_command_XY)
    TEXT_thread = Thread(target = run_command_text)

    XY_thread.start()
    TEXT_thread.start()

So, when i run these both the methods seperately, they do their work as expected but when i try to run they parallel and then print both the outputs, there seems to be an issue. (Please ignore indentation)

I've tried to provide a small example of running two threads and checking the output of a process in each of them. Both are doing the exact same thing at the moment but it should give you an idea.

import subprocess as sp
from threading import Thread

def func_x():
    cmd = 'python --version'
    for _ in range(5):
        proc = sp.Popen(cmd.split(), stdout=sp.PIPE)
        proc.wait()
        result = proc.stdout.read()
        print('func_x:', result.decode('utf-8').strip())

def func_y():
    cmd = 'python --version'
    for _ in range(5):
        proc = sp.Popen(cmd.split(), stdout=sp.PIPE)
        proc.wait()
        result = proc.stdout.read()
        print('func_y:', result.decode('utf-8').strip())

if __name__ == '__main__':
    x_thread = Thread(target=func_x)
    y_thread = Thread(target=func_y)

    x_thread.start()
    y_thread.start()

Which outputs:

func_y: Python 3.5.2
func_x: Python 3.5.2
func_y: Python 3.5.2
func_x: Python 3.5.2
func_y: Python 3.5.2
func_x: Python 3.5.2
func_x: Python 3.5.2
func_y: Python 3.5.2
func_y: Python 3.5.2
func_x: Python 3.5.2

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