views:

26

answers:

1

Hello,

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

+1  A: 

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

pyfunc
Thank you it seems that setblocking and settimeout may help me. The problem is that I want to modify the blocking or timeout value only at the moment I've sent every messages (which is when I'm already blocked in the recv operation)...
Martin Trigaux
@Martin Trigaux : As said, if you have already called recv, you will be blocked. You should either set to non-blocking or decrease locking timeout and design the solution around it.
pyfunc