如何将 'QUIT' 命令传递给已经 运行 python 的子进程

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

我正在使用 python 并调用子进程并逐行读取输出,它工作正常,如下所示。

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

我想向“jdebug”进程发出'bt'和“退出”命令当上述条件成立时退出进程。由于 jdebug 进程不 return 返回控制并且 python 程序需要显式发出 'Quit' 命令来取回控制。

想知道该怎么做吗?

我发送这个值: process.stdin.write('bt\n') process.stdin.write('quit\n')

你可以像这样杀死进程

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