溫馨提示×

python popen如何結束子進程

小樊
188
2024-06-15 16:19:26
欄目: 編程語言

使用subprocess.Popen.terminate()方法可以結束子進程。示例如下:

import subprocess

process = subprocess.Popen(["ping", "www.google.com"])

# 在一段時間后結束子進程
process.terminate()

如果需要等待子進程結束后再繼續執行主程序,可以使用subprocess.Popen.wait()方法:

import subprocess

process = subprocess.Popen(["ping", "www.google.com"])

# 等待子進程結束
process.wait()

如果需要強制結束子進程,可以使用subprocess.Popen.kill()方法:

import subprocess

process = subprocess.Popen(["ping", "www.google.com"])

# 強制結束子進程
process.kill()

0
亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女