簡體   English   中英

如何將“QUIT”命令傳遞給已經運行的 python 子進程

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

我正在使用 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

我想向“jdebug”進程發出“bt”和“quit”命令,當上述條件為真時退出該進程。 由於 jdebug 進程不會返回控制權,並且 python 程序需要顯式發出“退出”命令才能恢復控制權。

想知道如何做到這一點?

我發送這個值: process.stdin.write('bt\n') process.stdin.write('quit\n')

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

你可以像這樣殺死進程

process.kill()

您可以設置一個全局 boolean 變量,而不是將字符串發送到子進程,您可以使用此變量完成您的進程。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM