Leaks in subprocess.Popen

zloster v.vayer at gmail.com
Wed Sep 20 19:07:07 CEST 2006


I'm using Python 2.4.3 for Win32.
I was trying to run a few child processes simultaneously in separate
threads and get their STDOUT, but the program was leaking memory and I
found that it was because of subprocess operating in another thread.
The following code works fine, but I get a leaking handle every second.
You can see it in the task manager if you choose to see the <handle
count> column. Does anybody have a solution? Please help!

import subprocess, time, thread

def comm(command):
	run = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
	out, err = run.communicate()
	print out

while 1:
	thread.start_new_thread(comm, ("dir",))
	time.sleep(1)




More information about the Python-list mailing list