Leaks in subprocess.Popen

Discussion in 'Python' started by zloster, Sep 20, 2006.

  1. zloster

    zloster Guest

    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)
     
    zloster, Sep 20, 2006
    #1
    1. Advertising

  2. zloster wrote:
    > 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
     
    Ziga Seilnacht, Sep 20, 2006
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Michele Simionato

    subprocess.Popen

    Michele Simionato, Dec 12, 2004, in forum: Python
    Replies:
    5
    Views:
    17,779
  2. Ivo Woltring
    Replies:
    4
    Views:
    3,621
    Jeff Shannon
    Jan 27, 2005
  3. John Abel
    Replies:
    2
    Views:
    3,454
    John Abel
    May 12, 2005
  4. Earl Eiland
    Replies:
    3
    Views:
    546
    Peter Hansen
    May 16, 2005
  5. File.popen/IO.popen

    , May 20, 2006, in forum: Ruby
    Replies:
    1
    Views:
    272
    Robert Klemme
    May 20, 2006
Loading...

Share This Page