Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy does subprocess.Popen block the response of SimpleHTTPServer.SimpleHTTPRequestHandler
    text
    copied!<p>I have the strange problem that the return of a request in SimpleHTTPRequestHandler is blocked if in the request handler a new process is spawned by subprocess.Popen. But shouldn't Popen be asynchronous?</p> <p>The behavior is reproducible by the following files and using Python 2.6.7 on my OS X machine as well as ActivePython 2.6 on a SLES machine: webserver.py:</p> <pre><code>#!/usr/bin/env python import SimpleHTTPServer import SocketServer import subprocess import uuid class MyRequestHandler(SimpleHTTPServer.SimpleHTTPRequestHandler): def do_POST(self): print 'in do_POST' uuid_gen = str(uuid.uuid1()) subprocess.Popen(['python', 'sleep.py']) print 'subprocess spawned' return self.wfile.write(uuid_gen) Handler = MyRequestHandler server = SocketServer.TCPServer(('127.0.0.1', 9019), Handler) server.serve_forever() </code></pre> <p>sleep.py:</p> <pre><code>import time time.sleep(10) </code></pre> <p>When I now start the webserver and then do a curl POST request to localhost:9019 the webserver prints instantly:</p> <pre><code>$python2.6 webserver2.py in do_POST subprocess spawned </code></pre> <p>but on the console where I do the curl request it shows the following behavior:</p> <pre><code>$curl -X POST http://127.0.0.1:9019/ &lt;wait ~10 seconds&gt; cd8ee24a-0ad7-11e3-a361-34159e02ccec </code></pre> <p>When I run the same setup with Python 2.7 the answer arrives on the curl site instantly. How can this happen, since Popen doesn't seem to block the print, but just the return? The problem is I'm bound to python 2.6 for legacy reasons, so what would be the best workaround for that behavior to let the request return instantly?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload