client connects to old server thread and retrieves outdated data?
I have a client thread that will ping server for one of its status attributes:
def run ( self ):
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
cli开发者_如何学Pythonent.connect(('localhost',2727))
print 'client #', self.name , 'polling'
x = client.recv(1024)
client.close()
print x
my server runs a loop which gets it's state, and spawns a thread for the socket:
while True:
deviceState = self.getDeviceState()
channel, details = self.server.accept()
sThread.ServerThread(channel, details, deviceState).start()
The issue is that after I update the deviceState and the client pings, it first gets the old state (probably from an existing ServerThread) no matter how long I wait. Then when the client pings again, it can pick up the new state. Is there a way to shutdown the ServerThread if deviceState is updated and make sure the client socket gets a fresh server thread?
sThread.ServerThread()
is your function, right?
Instead of getting the device state and passing it as an argument to ServerThread()
, have your implementation of ServerThread()
do a call to .getDeviceState()
after the request comes in to get the latest device state.
The way you have it set up now returns the device state as it was when the thread was created, not the device state as it is when the request comes in.
精彩评论