I have a HTTP handler derived from BaseHTTPRequestHandler
class MyHandler(BaseHTTPRequestHandler):
do_GET():
...
The problem I've been having is that I'd like to report the state of my application running in another thread. It seems that for every request a new instance of handler is called so I can't keep my program state in MyHandler. I could store state globally but for design reasons I don't want to do that. Are there any other options?
You can use the multiprocessing module to accomplish this. First derive a server class:
class MyTCPServer(SocketServer.ForkingTCPServer):
manager = multiprocessing.Manager()
SHARED = manager.dict()
Then in your handler's do_GET you can do something like:
self.server.SHARED['foo'] = 1
Which should then be referable by other instance of do_GET.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With