Search code examples
pythonajaxmultithreadingcgisimultaneous

Python CGI simultaneous AJAX requests


So here's the deal : I'm writing a simple lightweight IRC app, hosted locally, that basically does the same job as Xchat and works in your browser, just as Sabnzbd. I display search results in the browser as an html table, and using an AJAX GET request with an on_click event, the download is launched. I use another AJAX GET request in a 1 second loop to request the download information (status, progress, speed, ETA, etc.). I hit a bump with the simultaneous AJAX requests, since my CGI handler seems to only be able to handle one thread at a time : indeed, the main thread processes the download, while requests for download status are sent too. Since I had a Django app somewhere, I tried implementing this IRC app and everything works fine. Simultaneous requests are handled properly. So is there something I have to know with the HTTP handler ? Is it not possible with the basic CGI handle to deal with simultaneous requests ? I use the following for my CGI IRC app :

from http.server import BaseHTTPRequestHandler, HTTPServer, CGIHTTPRequestHandler

If it's not about theory but about my code, I can gladly post various python scripts if it helps.


Solution

  • So, after further research, here's my code, whick works :

    from http.server import BaseHTTPRequestHandler, HTTPServer, CGIHTTPRequestHandler
    from socketserver import ThreadingMixIn
    import threading
    import cgitb; cgitb.enable()  ## This line enables CGI error reporting
    import webbrowser
    
    
    class HTTPRequestHandler(CGIHTTPRequestHandler):
        """Handle requests in a separate thread."""
        def do_GET(self):
            if "shutdown" in self.path:
                self.send_head()
                print ("shutdown")
                server.stop()
            else:
                self.send_head()
    
    
    class ThreadedHTTPServer(ThreadingMixIn, HTTPServer):
        allow_reuse_address = True
        daemon_threads = True
    
        def shutdown(self):
            self.socket.close()
            HTTPServer.shutdown(self)
    
    class SimpleHttpServer():
        def __init__(self, ip, port):
            self.server = ThreadedHTTPServer((ip,port), HTTPRequestHandler)
            self.status = 1
    
        def start(self):
            self.server_thread = threading.Thread(target=self.server.serve_forever)
            self.server_thread.daemon = True
            self.server_thread.start()
    
        def waitForThread(self):
            self.server_thread.join()
    
        def stop(self):
            self.server.shutdown()
            self.waitForThread()
    
    if __name__=='__main__':
        HTTPRequestHandler.cgi_directories = ["/", "/ircapp"]
        server = SimpleHttpServer('localhost', 8020)
        print ('HTTP Server Running...........')
        webbrowser.open_new_tab('http://localhost:8020/ircapp/search.py') 
        server.start()
        server.waitForThread()