結果を取得した後、PythonでPopenを介して発行したサービス(別のスレッドのバックグラウンドで実行)を停止する必要がありますが、次のアプローチは失敗しました(ping
説明のために使用してください):
class sample(threading.Thread):
def __init__(self, command, queue):
threading.Thread.__init__(self)
self.command = command;
self.queue = queue
def run(self):
result = Popen(self.command, shell=True, stdout=PIPE, stderr=STDOUT)
while True:
output = result.stdout.readline()
if not self.queue.empty():
result.kill()
break
if output != "":
print output
else:
break
def main():
q = Queue()
command = sample("ping 127.0.0.1", q)
command.start()
time.sleep(10)
q.put("stop!")
command.join()
if __name__ == "__main__":
main()
上記のプログラムを実行した後、 をpgrepすると、ping
まだそこにあります。Popenによって開かれたサブプロセスを強制終了するにはどうすればよいですか? ありがとう。
PS: result.terminate() も試しましたが、実際には問題は解決しません。