def infinite_sender(self, sender): """ Sends data until an exception happens. socket.error is caught, as it means the socket got closed. Sender is called to actually send the data. """ msgq = MsgQ() # We do only partial setup, so we don't create the listening socket msgq.setup_poller() (read, write) = socket.socketpair(socket.AF_UNIX, socket.SOCK_STREAM) msgq.register_socket(write) # Keep sending while it is not closed by the msgq try: while True: sender(msgq, write) except socket.error: pass # Explicitly close temporary socket pair as the Python # interpreter expects it. It may not be 100% exception safe, # but since this is only for tests we prefer brevity. read.close() write.close()