Why won't the pylons close the connection if the subprocess is running?

If I try to call the process from within the pylons controller , the server doesn't close the connection after sending the response.

Suppose it test.py

is a lengthy process, then this method in the pylons controller creates a response but keeps the connection open:

def index(self):
    from subprocess import Popen
    Popen(["python", "/temp/test.py"])

    return "<h1>Done!</h1>"

      

Moving Popen

to a new thread didn't help.

Luckily I found a workaround: I start a new thread and add sleep

before Popen

. If Popen is started after sending a response, the connection will be closed as expected.

def test(self):
    def worker():
        import time
        time.sleep(5)
        from subprocess import Popen
        Popen(["python", "/temp/test.py"])
    from threading import Thread
    Thread(target=worker).start()

    return "<h1>Done!</h1>"

      

Can anyone explain this behavior? I would like to be sure that I will not cause strange problems down the line.

I am using Python 2.5 and Pylons 0.9.6.1 on Windows XP SP3.

UPDATE : bnonlan's answer is definitely on the right track. Popen

has a parameter named close_fds

which should solve this problem. In Python 2.5 of the module, subprocess

this parameter is not supported on Windows. However, in Python 2.6, you can set this parameter to True

if you are not redirecting stdin / stdout / stderr.

def index(self):
    # I copied the 2.6 version of subprocess.py into my tree
    from python26.subprocess import Popen
    Popen(["python", "/temp/test.py"], close_fds=True)

    return "<h1>Done!</h1>"

      

Unfortunately I want to redirect stdout so I need to find another solution. Also, it means that the workaround I found could close other requests if they are executed when Popen is executed. This is troubling.

+1


a source to share





All Articles