Reputation: 5401
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
Upvotes: 2
Views: 2544
Reputation: 66729
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
socket.setblocking(0)
Set a timeout value see : http://docs.python.org/library/socket.html#socket.socket.settimeout
socket.settimeout(value)
Use asynchronous approach
Upvotes: 4