tenacity icon indicating copy to clipboard operation
tenacity copied to clipboard

Retry for Synchronous Generator

Open heri16 opened this issue 8 years ago • 5 comments

Could we support retry for Synchronous Generator?

import random
from tenacity import retry

@retry
def ticker(to):
    """Yield numbers from 0 to `to`."""
    for i in range(to):
        if random.randint(0, 10) > 1:
            raise IOError("Broken sauce, everything is hosed!!!111one")
        else:
            yield i

for i in ticker(10):
    print(i)

heri16 avatar Apr 23 '17 15:04 heri16

retry for generators breaks flow expectations, how would it correctly know what to do after say failure after the 5th item

RonnyPfannschmidt avatar Apr 23 '17 18:04 RonnyPfannschmidt

Good point. Generators uses exceptions internally. we would need to clone the generator first (itertools.tee) and then catch anything other than the StopIteration or GeneratorExit exception.

heri16 avatar Apr 25 '17 08:04 heri16

and that would have unexpected cost and side-effects as well

RonnyPfannschmidt avatar Apr 25 '17 16:04 RonnyPfannschmidt

Would you accept a documentation PR to clarify this?

untulis avatar Oct 20 '22 16:10 untulis