title: How to launch a pdftk subprocess while in wsgi?
body: <p>I need to launch a pdftk process while serving a web request in Django, and wait for it to finish.  My current pdftk code looks like this:</p>
  
  <pre><code>proc = subprocess.Popen(["/usr/bin/pdftk", 
                           "/tmp/infile1.pdf", 
                           "/tmp/infile2.pdf", 
                           "cat", "output", "/tmp/outfile.pdf"])    
  proc.communicate()
  </code></pre>
  
  <p>This works fine, as long as I'm executing under the dev server (running as user <code>www-data</code>).  But as soon as I switch to mod_wsgi, changing nothing else, the code hangs at <code>proc.communicate()</code>, and "outfile.pdf" is left as an open file handle of zero length.</p>
  
  <p>I've tried a several variants of the subprocess invocation (as well as plain old os.system) -- setting stdin/stdout/stderr to PIPE or to various file handles changes nothing.  Using "shell=True" prevents <code>proc.communicate()</code> from hanging, but then pdftk fails to create the output file, both under the devserver or mod_wsgi.  <a href="https://groups.google.com/group/modwsgi/browse_thread/thread/c29ad9fa9f516a93?fwc=1" rel="noreferrer">This discussion</a> seems to indicate there might be some deeper voodoo going on with OS signals and pdftk that I don't understand.</p>
  
  <p>Are there any workarounds to get a subprocess call like this to work properly under wsgi?  I'm avoiding using PyPDF to combine pdf files, because I have to combine large enough numbers of files (several hundred) that it runs out of memory (PyPDF needs to keep every source pdf file open in memory while combining them).</p>
  
  <p>I'm doing this under recent Ubuntu, pythons 2.6 and 2.7.</p>