rety.py 496 B

12345678910111213141516
  1. #!/usr/bin/env python
  2. # coding:utf-8
  3. def retry(times, exceptions=None):
  4. exceptions = exceptions if exceptions is not None else Exception
  5. def wrapper(func):
  6. def wrapper(*args, **kwargs):
  7. last_exception = None
  8. for _ in range(times):
  9. try:
  10. return func(*args, **kwargs)
  11. except exceptions as e:
  12. last_exception = e
  13. raise last_exception
  14. return wrapper
  15. return wrapper