self._stop = False

def __iter__(self):
    return self

def stop(self):
    self._stop = True

def next(self):
    if self._stop:
        raise StopIteration
return next(self._iter)
Sending StopIteration to for loop from outside of the iterator