Here is the code in question:
class Server(SocketServer.ForkingMixIn, SocketServer.TCPServer):
__slots__ = ("loaded")
class Handler(SocketServer.StreamRequestHandler):
def handle(self):
print self.server.loaded
self.server.loaded = True
print self.server.loaded
server = Server(('localhost', port), Handler)
server.loaded = False
while True:
server.handle_request()
Every time a new request arrives, I get the output False, which follows True. I want False, and then Truefor the first time True, and then True.
Why not the changes made to the variable in the server instance, which is saved outside the handler function handle()?
UPDATED:
So, I'm trying to use global variables to achieve what I want:
loaded = False
class Server(SocketServer.ForkingMixIn, SocketServer.TCPServer):
pass
class Handler(SocketServer.StreamRequestHandler):
def handle(self):
global loaded
print loaded
loaded = True
print loaded
def main():
server = Server(('localhost', 4444), Handler)
global loaded
loaded = False
while True:
server.handle_request()
if (__name__ == '__main__'):
main()
And it still does not work, i.e. produces the same result as before. Can someone tell me where I am going wrong?