I’m new to Python and I’m having some issues with asynchronous calls and webservers.
I have a SimpleHTTPServer communicating via AJAX with a website that lets you start and stop a service in the host.
The problem is that I don’t get my HTTP:201 until the call has ended, which never happens since this is a long running process.
I have tried with call, having my cmd inside a shell script that executes ‘cmd &’ also tried with Popen, which I though is was non-blocking.
I also tried with thread.start_new_thread(os.system, (cmd,))
def do_POST(self):
# ... some code to get args ...
subprocess.Popen([cmd, args])
# I'd like 'cmd args' to run in the server while I return my HTTP:201 here
self.send_response(201)
self.end_headers()
self.wfile.write(output)
I found the solution here:
Why does a background task block the response in SimpleHTTPServer?
Else python holds the TCP connection open until the background command finishes.
Specifying the length of your response, python send the TCP FIN and your browser can carry on with its life 🙂