Skip to content

Conversation

@abloomston
Copy link

Added the ability to provide retry logic for a function that is a generator (uses yield). This addition to README.rst:

vals = [RuntimeError(0), 1, 2, RuntimeError(3), 4]

@retry(generator=True)
def make_trouble():
    for v in vals:
        if isinstance(v, BaseException):
            vals.remove(v)
            raise v
        else:
            yield v

if __name__ == '__main__':
    # [1, 2, 1, 2, 4]
    # Actually:
    # <Initial call>
    # <New call due to RuntimeError(0)>
    # 1, 2
    # <New call due to RuntimeError(3)>
    # 1, 2, 4
    print([x for x in make_trouble()])

Can be thought of as simulating a generator that is connecting to a third party service with intermittent failures (RuntimeError(0) after receiving 1, 2, followed by RuntimeError(3) after restarting and retrieving 1, 2, 4). This allows for graceful retrying, allowing the user of the generator to be responsible for handling duplicates.

The use of an optional parameter generator=False on the decorator is meant to be fully backwards compatible.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant