Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python subprocess kill doesn't release the file handle

When you start a windows executable using subprocess.Popen in shell mode and kill it (instead of letting the exe finish running), the executable remains locked for as long as the python program is running. This doesn't happen when shell=False. Anyone know a solution for this?

import time
import subprocess
proc = subprocess.Popen(r"path_to_executable", shell=True)
time.sleep(1)
proc.terminate() #or kill()
proc = None
# the executable will be locked for the next 100 secs
time.sleep(100)
like image 802
kefeizhou Avatar asked Mar 05 '26 09:03

kefeizhou


1 Answers

You are terminating the shell and not the process it spawned. That is my guess. If that's right, have a look at this question:

subprocess: deleting child processes in Windows

like image 99
rplnt Avatar answered Mar 07 '26 22:03

rplnt



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!