_synchronization.py 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. from __future__ import annotations
  2. import threading
  3. import types
  4. from ._exceptions import ExceptionMapping, PoolTimeout, map_exceptions
  5. # Our async synchronization primatives use either 'anyio' or 'trio' depending
  6. # on if they're running under asyncio or trio.
  7. try:
  8. import trio
  9. except (ImportError, NotImplementedError): # pragma: nocover
  10. trio = None # type: ignore
  11. try:
  12. import anyio
  13. except ImportError: # pragma: nocover
  14. anyio = None # type: ignore
  15. def current_async_library() -> str:
  16. # Determine if we're running under trio or asyncio.
  17. # See https://sniffio.readthedocs.io/en/latest/
  18. try:
  19. import sniffio
  20. except ImportError: # pragma: nocover
  21. environment = "asyncio"
  22. else:
  23. environment = sniffio.current_async_library()
  24. if environment not in ("asyncio", "trio"): # pragma: nocover
  25. raise RuntimeError("Running under an unsupported async environment.")
  26. if environment == "asyncio" and anyio is None: # pragma: nocover
  27. raise RuntimeError(
  28. "Running with asyncio requires installation of 'httpcore[asyncio]'."
  29. )
  30. if environment == "trio" and trio is None: # pragma: nocover
  31. raise RuntimeError(
  32. "Running with trio requires installation of 'httpcore[trio]'."
  33. )
  34. return environment
  35. class AsyncLock:
  36. """
  37. This is a standard lock.
  38. In the sync case `Lock` provides thread locking.
  39. In the async case `AsyncLock` provides async locking.
  40. """
  41. def __init__(self) -> None:
  42. self._backend = ""
  43. def setup(self) -> None:
  44. """
  45. Detect if we're running under 'asyncio' or 'trio' and create
  46. a lock with the correct implementation.
  47. """
  48. self._backend = current_async_library()
  49. if self._backend == "trio":
  50. self._trio_lock = trio.Lock()
  51. elif self._backend == "asyncio":
  52. self._anyio_lock = anyio.Lock()
  53. async def __aenter__(self) -> AsyncLock:
  54. if not self._backend:
  55. self.setup()
  56. if self._backend == "trio":
  57. await self._trio_lock.acquire()
  58. elif self._backend == "asyncio":
  59. await self._anyio_lock.acquire()
  60. return self
  61. async def __aexit__(
  62. self,
  63. exc_type: type[BaseException] | None = None,
  64. exc_value: BaseException | None = None,
  65. traceback: types.TracebackType | None = None,
  66. ) -> None:
  67. if self._backend == "trio":
  68. self._trio_lock.release()
  69. elif self._backend == "asyncio":
  70. self._anyio_lock.release()
  71. class AsyncThreadLock:
  72. """
  73. This is a threading-only lock for no-I/O contexts.
  74. In the sync case `ThreadLock` provides thread locking.
  75. In the async case `AsyncThreadLock` is a no-op.
  76. """
  77. def __enter__(self) -> AsyncThreadLock:
  78. return self
  79. def __exit__(
  80. self,
  81. exc_type: type[BaseException] | None = None,
  82. exc_value: BaseException | None = None,
  83. traceback: types.TracebackType | None = None,
  84. ) -> None:
  85. pass
  86. class AsyncEvent:
  87. def __init__(self) -> None:
  88. self._backend = ""
  89. def setup(self) -> None:
  90. """
  91. Detect if we're running under 'asyncio' or 'trio' and create
  92. a lock with the correct implementation.
  93. """
  94. self._backend = current_async_library()
  95. if self._backend == "trio":
  96. self._trio_event = trio.Event()
  97. elif self._backend == "asyncio":
  98. self._anyio_event = anyio.Event()
  99. def set(self) -> None:
  100. if not self._backend:
  101. self.setup()
  102. if self._backend == "trio":
  103. self._trio_event.set()
  104. elif self._backend == "asyncio":
  105. self._anyio_event.set()
  106. async def wait(self, timeout: float | None = None) -> None:
  107. if not self._backend:
  108. self.setup()
  109. if self._backend == "trio":
  110. trio_exc_map: ExceptionMapping = {trio.TooSlowError: PoolTimeout}
  111. timeout_or_inf = float("inf") if timeout is None else timeout
  112. with map_exceptions(trio_exc_map):
  113. with trio.fail_after(timeout_or_inf):
  114. await self._trio_event.wait()
  115. elif self._backend == "asyncio":
  116. anyio_exc_map: ExceptionMapping = {TimeoutError: PoolTimeout}
  117. with map_exceptions(anyio_exc_map):
  118. with anyio.fail_after(timeout):
  119. await self._anyio_event.wait()
  120. class AsyncSemaphore:
  121. def __init__(self, bound: int) -> None:
  122. self._bound = bound
  123. self._backend = ""
  124. def setup(self) -> None:
  125. """
  126. Detect if we're running under 'asyncio' or 'trio' and create
  127. a semaphore with the correct implementation.
  128. """
  129. self._backend = current_async_library()
  130. if self._backend == "trio":
  131. self._trio_semaphore = trio.Semaphore(
  132. initial_value=self._bound, max_value=self._bound
  133. )
  134. elif self._backend == "asyncio":
  135. self._anyio_semaphore = anyio.Semaphore(
  136. initial_value=self._bound, max_value=self._bound
  137. )
  138. async def acquire(self) -> None:
  139. if not self._backend:
  140. self.setup()
  141. if self._backend == "trio":
  142. await self._trio_semaphore.acquire()
  143. elif self._backend == "asyncio":
  144. await self._anyio_semaphore.acquire()
  145. async def release(self) -> None:
  146. if self._backend == "trio":
  147. self._trio_semaphore.release()
  148. elif self._backend == "asyncio":
  149. self._anyio_semaphore.release()
  150. class AsyncShieldCancellation:
  151. # For certain portions of our codebase where we're dealing with
  152. # closing connections during exception handling we want to shield
  153. # the operation from being cancelled.
  154. #
  155. # with AsyncShieldCancellation():
  156. # ... # clean-up operations, shielded from cancellation.
  157. def __init__(self) -> None:
  158. """
  159. Detect if we're running under 'asyncio' or 'trio' and create
  160. a shielded scope with the correct implementation.
  161. """
  162. self._backend = current_async_library()
  163. if self._backend == "trio":
  164. self._trio_shield = trio.CancelScope(shield=True)
  165. elif self._backend == "asyncio":
  166. self._anyio_shield = anyio.CancelScope(shield=True)
  167. def __enter__(self) -> AsyncShieldCancellation:
  168. if self._backend == "trio":
  169. self._trio_shield.__enter__()
  170. elif self._backend == "asyncio":
  171. self._anyio_shield.__enter__()
  172. return self
  173. def __exit__(
  174. self,
  175. exc_type: type[BaseException] | None = None,
  176. exc_value: BaseException | None = None,
  177. traceback: types.TracebackType | None = None,
  178. ) -> None:
  179. if self._backend == "trio":
  180. self._trio_shield.__exit__(exc_type, exc_value, traceback)
  181. elif self._backend == "asyncio":
  182. self._anyio_shield.__exit__(exc_type, exc_value, traceback)
  183. # Our thread-based synchronization primitives...
  184. class Lock:
  185. """
  186. This is a standard lock.
  187. In the sync case `Lock` provides thread locking.
  188. In the async case `AsyncLock` provides async locking.
  189. """
  190. def __init__(self) -> None:
  191. self._lock = threading.Lock()
  192. def __enter__(self) -> Lock:
  193. self._lock.acquire()
  194. return self
  195. def __exit__(
  196. self,
  197. exc_type: type[BaseException] | None = None,
  198. exc_value: BaseException | None = None,
  199. traceback: types.TracebackType | None = None,
  200. ) -> None:
  201. self._lock.release()
  202. class ThreadLock:
  203. """
  204. This is a threading-only lock for no-I/O contexts.
  205. In the sync case `ThreadLock` provides thread locking.
  206. In the async case `AsyncThreadLock` is a no-op.
  207. """
  208. def __init__(self) -> None:
  209. self._lock = threading.Lock()
  210. def __enter__(self) -> ThreadLock:
  211. self._lock.acquire()
  212. return self
  213. def __exit__(
  214. self,
  215. exc_type: type[BaseException] | None = None,
  216. exc_value: BaseException | None = None,
  217. traceback: types.TracebackType | None = None,
  218. ) -> None:
  219. self._lock.release()
  220. class Event:
  221. def __init__(self) -> None:
  222. self._event = threading.Event()
  223. def set(self) -> None:
  224. self._event.set()
  225. def wait(self, timeout: float | None = None) -> None:
  226. if timeout == float("inf"): # pragma: no cover
  227. timeout = None
  228. if not self._event.wait(timeout=timeout):
  229. raise PoolTimeout() # pragma: nocover
  230. class Semaphore:
  231. def __init__(self, bound: int) -> None:
  232. self._semaphore = threading.Semaphore(value=bound)
  233. def acquire(self) -> None:
  234. self._semaphore.acquire()
  235. def release(self) -> None:
  236. self._semaphore.release()
  237. class ShieldCancellation:
  238. # Thread-synchronous codebases don't support cancellation semantics.
  239. # We have this class because we need to mirror the async and sync
  240. # cases within our package, but it's just a no-op.
  241. def __enter__(self) -> ShieldCancellation:
  242. return self
  243. def __exit__(
  244. self,
  245. exc_type: type[BaseException] | None = None,
  246. exc_value: BaseException | None = None,
  247. traceback: types.TracebackType | None = None,
  248. ) -> None:
  249. pass