Hacker News new | ask | show | jobs
by masklinn 3095 days ago
> the `while True` will never terminate

The iterator is infinite on purpose (it has no reason to be finite).

You'd terminate it from outside, either when the operation succeeds, so the example loop should more properly be something along the lines of:

    for _ in backoff():
        try:
            c = connect_to_a_thing()
            break
        except ConnectionError:
            pass # retry
or you'd use composition to e.g. stop after a set number of tries:

    for _ in itertools.islice(backoff(), 5):
        # do stuff
most likely a combination of both.