Why Does A Background Task Block The Response In Simplehttpserver?
Solution 1:
In addition to Steve's and my comments above, here is a solution that works for me.
The method to determine a content-length is a bit ugly. If you don't specify one, the browser may still show a spinning cursor although the content is shown. Closing the self.wfile
instead could also work.
from cStringIO import StringIO
classPage(SimpleHTTPServer.SimpleHTTPRequestHandler):
defdo_GET(self):
out = StringIO()
self.send_response(200)
self.send_header("Content-type", "text/html")
if self.path == '/':
out.write("<html><body><a href='/run'>Run</a></body></html>\n")
elif self.path == '/run':
self.proc = MyProc()
print'Starting..'
self.proc.start()
print'After start.'
out.write("<html><body><h1>Process started</h1></body></html>\n")
text = out.getvalue()
self.send_header("Content-Length", str(len(text)))
self.end_headers()
self.wfile.write(text)
Solution 2:
I use this snippet to run Threaded Version of SimpleHTTPServer.
I save this file as ThreadedHTTPServer.py
for example and then I run like that:
$ python -m /path/to/ThreadedHTTPServer PORT
So it'll be threated in separated threads and now you can download in paralell and navigate properly.
from BaseHTTPServer import HTTPServer, BaseHTTPRequestHandler
from SocketServer import ThreadingMixIn
import threading
import SimpleHTTPServer
import sys
PORT = int(sys.argv[1])
Handler = SimpleHTTPServer.SimpleHTTPRequestHandler
classThreadedHTTPServer(ThreadingMixIn, HTTPServer):
"""Handle requests in a separate thread."""if __name__ == '__main__':
server = ThreadedHTTPServer(('0.0.0.0', PORT), Handler)
print'Starting server, use <Ctrl-C> to stop'
server.serve_forever()
Solution 3:
The answer is that the multiprocessing module forks a completely different process with its own stdout... So your application is running just as you wrote it:
- You start up the application in your terminal window.
- You click on the Run button in your browser which does a GET on /run
- You see the output of the current process in your terminal window, "Starting.."
- A new process is started, MyProc with its own stdout and stderr.
- MyProc prints to its stdout (which goes nowhere), 'Starting long process..'.
- The very moment MyProc starts up, your app prints to stdout, "After start." since it was not told to wait for any kind of response from MyProc before doing so.
What you need is to implement a Queue that communicates back and forth between your main application's process and the forked process. There's some multiprocessing-specific examples on how to do that here:
http://www.ibm.com/developerworks/aix/library/au-multiprocessing/
However, that article (like most articles from IBM) is kind of deep and overly complicated... You might want to take a look at a simpler example of how to use the "regular" Queue module (it is pretty much identical to the one included in multiprocessing):
http://www.artfulcode.net/articles/multi-threading-python/
The most important concepts to understand are how to shuffle data between processes using the Queue and how to use join() to wait for a response before proceeding.
Post a Comment for "Why Does A Background Task Block The Response In Simplehttpserver?"