_nretry.py 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. #
  2. # Copyright 2022 The FATE Authors. All Rights Reserved.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. #
  16. import time
  17. from fate_arch.common.log import getLogger
  18. LOGGER = getLogger()
  19. def nretry(func):
  20. """retry connection
  21. """
  22. def wrapper(self, *args, **kwargs):
  23. """wrapper
  24. """
  25. res = None
  26. exception = None
  27. for ntry in range(10):
  28. try:
  29. res = func(self, *args, **kwargs)
  30. exception = None
  31. break
  32. except Exception as e:
  33. LOGGER.error("function %s error" % func.__name__, exc_info=True)
  34. exception = e
  35. time.sleep(1)
  36. if exception is not None:
  37. LOGGER.debug(
  38. f"failed",
  39. exc_info=exception)
  40. raise exception
  41. return res
  42. return wrapper