I am trying to create a python server that needs to use some local data when handling requests. Here is my code:
httpd = HTTPServer((HOST_NAME,PORT),myHandler)
print(time.asctime(), "Start Server - %s:%s\n"%(HOST_NAME,PORT))
try:
httpd.serve_forever()
except KeyboardInterrupt:
pass
httpd.server_close()
print(time.asctime(),'Stop Server - %s:%s' %(HOST_NAME,PORT))
I have tried loading the local data in myHandler class, but it seems that everytime a new request is made the server reinitializes a new instance of myHandler, hence reloading the data. (which slows down the server significantly).
The myHandler class is currently written in a separate python script, so I can't create global variables on the main function.
Would it work if I merge the main script and the myHandler script so I can use global variables? Is there a better way? Thank you for your solutions.
That's correct. A handler is associated with a request. If you have data that should persist for the lifetime of your server, you need to make it part of the server class. E.g., we can subclass
HTTPServerlike this:Here, we have a piece of data (
self.myData) associated with the server. We can reference this in a handler by using theself.serverattribute to access the server instance.