Starting a process in one HTTP call and getting results in another

Posted by KillianDS on Stack Overflow See other posts from Stack Overflow or by KillianDS
Published on 2010-04-28T21:37:13Z Indexed on 2010/04/28 23:37 UTC
Read the original article Hit count: 321

Filed under:
|
|

Hi, I'm writing a very simple testing framework for my application, the design isn't perfect, but I don't have time to write something more complex.

Essentially, I have a client and server-application, on my server I want a small python web server to start the server application with given test sequences on a GET or POST call. Also, the application prints some testdata to stderr which I'd like to catch and return in another HTTP call. At the moment I have this:

from subprocess import Popen, PIPE
from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer

p = None
class MyHandler(BaseHTTPRequestHandler):
    def do_GET(self):
        global p
        if self.path.endswith("start/"):
            p = Popen(["./bin/Release/simplex264","BBB-360","127.0.0.1"], stderr=PIPE)
            print 'started'
            return
        elif self.path.endswith("getResults/"):
            self.wfile.write(p.stderr.read())
            return
        self.send_error(404,'File Not Found: %s' % self.path)

def main():
    try:
        server = HTTPServer(('localhost', 9876), MyHandler)
        print 'Started server...'
        server.serve_forever()
    except KeyboardInterrupt:
        print 'Shutting down...'
        server.socket.close()

if __name__ == '__main__':
    main()

Which 'works', except for one part, when I try to open http://localhost:9876/start/, it does not return before the process ended. However, the 'started' appears in my shell immediately (I added this because I thought the Popen call would only return after execution). I do not know the perfect inner workings of Popen and BaseHTTPRequestHandler however and do not really know where it goes wrong. Is there any way to make this work asynchronously?

© Stack Overflow or respective owner

Related posts about python

Related posts about popen