6
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Python3のsubprocessでプロセスが殺せない

6
Posted at

前提

Pythonのsubprocessで実行ファイルに時間制限をつけて実行していた.
一覧からなぜかプロセスが消えないため調査した.

発生した環境

この記事を参考にしました

import subprocess
import time

p=subprocess.Popen('a.exe args',shell=True)
time.sleep(5)
p.kill()

プロセスが生き続けているため,無限ループの場合ずっと生きていることになる
UNIX環境の場合は親のPythonが終了すると子となっているプロセスも落ちるようだ.

解決方法

- p=subprocess.Popen('a.exe args',shell=True)
+ p=subprocess.Popen(['a.exe', 'args'],shell=False)

シェルモードをFalseにする.

参考

6
2
3

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?