Skip to content Skip to sidebar Skip to footer

Subprocess.popen Hangs With Interactive Programs When Called From Inside Django

I have written a small Django App, that executes an interactive program based on user input and returns the output as the result. But for some reason, the subprocess hangs. On veri

Solution 1:

The subprocess documentation suggests that you "use communicate() rather than .stdin.write, .stdout.read or .stderr.read to avoid deadlocks due to any of the other OS pipe buffers filling up and blocking the child process."

Does it work if you use return p.communicate()[0] instead of return p.stdout.read()?

Solution 2:

I've had the same issue doing this sort of thing to get the mercurial tip version:

import subprocess

lProcess = subprocess.Popen(["hg","tip"], stdin=subprocess.PIPE, stdout=subprocess.PIPE)
lOutput = lProcess.stdout.readline()
lTipRevision = lOutput[10:].strip()
lTipRevision = lTipRevision[:lTipRevision.find(":")].strip()
print"Repository tip version is %s" % lTipRevision

This works fine when running through apache (mod_wsgi), but causes blank pages in the development server.

I've had a wander around the bugs related to this and the ones I could find seems to be closed as duplicate, or closed worksforme.

My original post about this is available at http://groups.google.com/group/django-users/browse_thread/thread/147ffe75899fd6e?pli=1

Solution 3:

I think your problem is the directory where the 'runtest' program is located. You view is going to fetch that module in the same directory where the view is. You can also specify 'cwd' argument in the Popen list of arguments if the module is anywhere else. I'm using Popen command in a view with Django dev server whithout any problems, so the dev server is not the cause of your problem.

Solution 4:

My problem was in the fact I was running Django in Pycharm. Running ./manage.py runserver directly fixed it for me.

Post a Comment for "Subprocess.popen Hangs With Interactive Programs When Called From Inside Django"