Saturday, March 26, 2016

Re: Simple subprocess help

If I understand you correctly, you want to pipe the value of request.POST['post'] to the program and store the output in a field of your instance.

You are calling subprocess.Popen incorrectly. It should be:

p = subprocess.Popen(['/path/to/'], stdin=subprocess.PIPE, stdout=subprocess.PIPE)

Then pass in the input and read the output

(stdoutdata, stderrdata) = p.communicate()

Then save the data, e.g. in a field of your instance

instance.processed_data = stdoutdata

I suggest you first make sure to get the call to the subprocess working in a Python shell and then integrate it in your Django app.
Please note that creating a (potentially long-running) subprocess in a request is really bad practice and can lead to a lot of problems. The best practice is to delegate long-running tasks to a job queue. For Django, Celery is probably most commonly used. There is a bit of setup involved, though.


P.S.: I just answered this question on StackOverflow. I'm reposting my answer here hoping that it might help anyone else.

On Friday, March 25, 2016 at 7:03:10 PM UTC+1, Joshua Valdez wrote:

Okay, so I have figured out that the subprocess is the module that I want to use in this context and I have tried implementing some simple code based on the documentation but I am getting an

Exception Type: OSError  Exception Value: [Errno 2] No such file or directory  Exception Location: /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ in _execute_child, line 1335.  

A brief overview of what I'm trying to do:

This is the code I have in views. Its intent is to take text input from the model form, POST that to the DB and then pass that input into my script which produces an XML file which is stored in another column in the DB. I'm very new to django so I'm sorry if this is an simple fix, but I couldn't find any documentation relating django to subprocess that was helpful.

def queries_create(request):      if not request.user.is_authenticated():         return render(request, 'login_error.html')          form = QueryForm(request.POST or None)      if form.is_valid():        instance =        p=subprocess.Popen([request.POST['post'], './path/to/'])        context = {          "title":"Create",        "form": form,        }      return render(request, "query_form.html", context) 

Model code snippet:

class Query(models.Model):    problem/intervention = models.TextField()      updated = models.DateTimeField(auto_now=True, auto_now_add=False)    timestamp = models.DateTimeField(auto_now=False, auto_now_add=True)

You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to
To post to this group, send email to
Visit this group at
To view this discussion on the web visit
For more options, visit

No comments:

Post a Comment