简体   繁体   English

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

[英]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.我正在使用 python 并调用子进程并逐行读出 put ,它工作正常,如下所示。

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.我想向“jdebug”进程发出“bt”和“quit”命令,当上述条件为真时退出该进程。 Since jdebug process don't return back the control and python program needs to explicitly issue 'Quit' command to get the control back.由于 jdebug 进程不会返回控制权,并且 python 程序需要显式发出“退出”命令才能恢复控制权。

Wondering how to do that?想知道如何做到这一点?

I am sending this value: process.stdin.write('bt\n') process.stdin.write('quit\n')我发送这个值: 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.您可以设置一个全局 boolean 变量,而不是将字符串发送到子进程,您可以使用此变量完成您的进程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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