To implement a simple protocol in python, I’ve used a thread to monitor acknowledgements send by the receiver. To do this, I use a thread in a function
def ackListener(self):
while self.waitingforack:
ack = self.socket_agent.recv(4)
...
exit()
where self.waitingforack is a boolean I set to False when I’ve received an acknowledgement for every messages I’ve sent.
The problem is that my code is blocking at the self.socket_agent.recv(4) operation, the modification of the value of waitingforack is done too late
Is there a way to force the Thread to terminate when I’m not waiting for ack anymore ?
Thank you
Remember that these operations like recv are blocking operations in your case.
So calling this function blocks unless you have received the data.
There are two ways to go about it:
Make socket non blocking
Set a timeout value
see : http://docs.python.org/library/socket.html#socket.socket.settimeout
Use asynchronous approach