简体   繁体   中英

How to pass the 'QUIT' command to the already running python sub process

I am using python and invoking sub process and reading out put line by line and it is working fine as shown below.

process = subprocess.Popen(['jdebug', 'File.tgz'], 
                           stdout=subprocess.PIPE,
                           universal_newlines=True)

while True:
    output = process.stdout.readline()
    print(output.strip())
    return_code = process.poll()
    print( return_code)
    if "lastdata" in str(output):         <- How to send 'bt' and 'quit' command at this point and read the response back.
      process.communicate('bt')
      process.communicate('quit')
if return_code is not None:

    # Process has finished, read rest of the output
    
    for output in process.stdout.readlines():
        
        print(output.strip())
    break

I want to issue 'bt' and "quit" command to the "jdebug" process When the above condition is true to quit the process. Since jdebug process don't return back the control and python program needs to explicitly issue 'Quit' command to get the control back.

Wondering how to do that?

I am sending this value: process.stdin.write('bt\n') process.stdin.write('quit\n')

# write on the stdin out of your process 
subprocess.stdin.write('QUIT')

You can just kill process, like this

process.kill()

Instead of sending a string to subprocess, you could set a global boolean variable and you can finish your process with this variable.

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