tornadoweb.py 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2017 Elisey Zanko
  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. import sys
  16. from pip._vendor.tenacity import BaseRetrying
  17. from pip._vendor.tenacity import DoAttempt
  18. from pip._vendor.tenacity import DoSleep
  19. from pip._vendor.tenacity import RetryCallState
  20. from tornado import gen
  21. class TornadoRetrying(BaseRetrying):
  22. def __init__(self, sleep=gen.sleep, **kwargs):
  23. super(TornadoRetrying, self).__init__(**kwargs)
  24. self.sleep = sleep
  25. @gen.coroutine
  26. def __call__(self, fn, *args, **kwargs):
  27. self.begin(fn)
  28. retry_state = RetryCallState(retry_object=self, fn=fn, args=args, kwargs=kwargs)
  29. while True:
  30. do = self.iter(retry_state=retry_state)
  31. if isinstance(do, DoAttempt):
  32. try:
  33. result = yield fn(*args, **kwargs)
  34. except BaseException: # noqa: B902
  35. retry_state.set_exception(sys.exc_info())
  36. else:
  37. retry_state.set_result(result)
  38. elif isinstance(do, DoSleep):
  39. retry_state.prepare_for_next_attempt()
  40. yield self.sleep(do)
  41. else:
  42. raise gen.Return(do)