m2m模型翻译
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

94 lines
3.5 KiB

6 months ago
  1. # Copyright 2016 Étienne Bersac
  2. # Copyright 2016 Julien Danjou
  3. # Copyright 2016 Joshua Harlow
  4. # Copyright 2013-2014 Ray Holder
  5. #
  6. # Licensed under the Apache License, Version 2.0 (the "License");
  7. # you may not use this file except in compliance with the License.
  8. # You may obtain a copy of the License at
  9. #
  10. # http://www.apache.org/licenses/LICENSE-2.0
  11. #
  12. # Unless required by applicable law or agreed to in writing, software
  13. # distributed under the License is distributed on an "AS IS" BASIS,
  14. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. # See the License for the specific language governing permissions and
  16. # limitations under the License.
  17. import functools
  18. import sys
  19. import typing as t
  20. from asyncio import sleep
  21. from pip._vendor.tenacity import AttemptManager
  22. from pip._vendor.tenacity import BaseRetrying
  23. from pip._vendor.tenacity import DoAttempt
  24. from pip._vendor.tenacity import DoSleep
  25. from pip._vendor.tenacity import RetryCallState
  26. WrappedFnReturnT = t.TypeVar("WrappedFnReturnT")
  27. WrappedFn = t.TypeVar("WrappedFn", bound=t.Callable[..., t.Awaitable[t.Any]])
  28. class AsyncRetrying(BaseRetrying):
  29. sleep: t.Callable[[float], t.Awaitable[t.Any]]
  30. def __init__(self, sleep: t.Callable[[float], t.Awaitable[t.Any]] = sleep, **kwargs: t.Any) -> None:
  31. super().__init__(**kwargs)
  32. self.sleep = sleep
  33. async def __call__( # type: ignore[override]
  34. self, fn: WrappedFn, *args: t.Any, **kwargs: t.Any
  35. ) -> WrappedFnReturnT:
  36. self.begin()
  37. retry_state = RetryCallState(retry_object=self, fn=fn, args=args, kwargs=kwargs)
  38. while True:
  39. do = self.iter(retry_state=retry_state)
  40. if isinstance(do, DoAttempt):
  41. try:
  42. result = await fn(*args, **kwargs)
  43. except BaseException: # noqa: B902
  44. retry_state.set_exception(sys.exc_info()) # type: ignore[arg-type]
  45. else:
  46. retry_state.set_result(result)
  47. elif isinstance(do, DoSleep):
  48. retry_state.prepare_for_next_attempt()
  49. await self.sleep(do)
  50. else:
  51. return do # type: ignore[no-any-return]
  52. def __iter__(self) -> t.Generator[AttemptManager, None, None]:
  53. raise TypeError("AsyncRetrying object is not iterable")
  54. def __aiter__(self) -> "AsyncRetrying":
  55. self.begin()
  56. self._retry_state = RetryCallState(self, fn=None, args=(), kwargs={})
  57. return self
  58. async def __anext__(self) -> AttemptManager:
  59. while True:
  60. do = self.iter(retry_state=self._retry_state)
  61. if do is None:
  62. raise StopAsyncIteration
  63. elif isinstance(do, DoAttempt):
  64. return AttemptManager(retry_state=self._retry_state)
  65. elif isinstance(do, DoSleep):
  66. self._retry_state.prepare_for_next_attempt()
  67. await self.sleep(do)
  68. else:
  69. raise StopAsyncIteration
  70. def wraps(self, fn: WrappedFn) -> WrappedFn:
  71. fn = super().wraps(fn)
  72. # Ensure wrapper is recognized as a coroutine function.
  73. @functools.wraps(fn)
  74. async def async_wrapped(*args: t.Any, **kwargs: t.Any) -> t.Any:
  75. return await fn(*args, **kwargs)
  76. # Preserve attributes
  77. async_wrapped.retry = fn.retry # type: ignore[attr-defined]
  78. async_wrapped.retry_with = fn.retry_with # type: ignore[attr-defined]
  79. return async_wrapped # type: ignore[return-value]