Leaks in subprocess.Popen

Z

zloster

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)
 
Z

Ziga Seilnacht

zloster said:
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!

This bug is fixed in the 2.5 version of Python and will be fixed in the
next 2.4 maintainance release (2.4.4). See:
http://www.python.org/sf/1500293 for the bug report.

You can find the relevant changes here:
http://mail.python.org/pipermail/python-checkins/2006-June/053417.html
http://mail.python.org/pipermail/python-checkins/2006-June/053418.html

If you have Python Win32 Extensions installed you can try using that
instead of the extension in the standard library; you have to change a
single line in the subprocess module:

--- subprocess_modified.py 2006-09-20 22:04:29.734375000 +0200
+++ subprocess.py 2006-09-20 22:01:52.296875000 +0200
@@ -350,7 +350,7 @@
if mswindows:
import threading
import msvcrt
- if 0: # <-- change this to use pywin32 instead of the _subprocess
driver
+ if 1: # <-- change this to use pywin32 instead of the _subprocess
driver
import pywintypes
from win32api import GetStdHandle, STD_INPUT_HANDLE, \
STD_OUTPUT_HANDLE, STD_ERROR_HANDLE



Hope this helps,
Ziga
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,119
Latest member
IrmaNorcro
Top