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