Instead of writing complicated try-catch constructs to #retry things, using auto-retrying function #decorators certainly changed my life to the better.
This is using #SQLAlchemy & the #backoff library https://pypi.org/project/backoff/ to retry generating a unique token, in the rare event that we chose a token that’s already in the database. On an IntegrityError, the function will “call itself” again.
(Event hooks & exponential backoff are available too.)