|
1 | 1 | """An S3-based caching core for cachier.""" |
2 | 2 |
|
| 3 | +import asyncio |
3 | 4 | import pickle |
4 | 5 | import time |
5 | 6 | import warnings |
@@ -28,6 +29,10 @@ class MissingS3Bucket(ValueError): |
28 | 29 | class _S3Core(_BaseCore): |
29 | 30 | """S3-based core for Cachier, supporting AWS S3 and S3-compatible backends. |
30 | 31 |
|
| 32 | + Async support in this core is delegated rather than native. Since boto3 is a |
| 33 | + synchronous client, async methods explicitly offload I/O work to a thread via |
| 34 | + ``asyncio.to_thread``. |
| 35 | +
|
31 | 36 | Parameters |
32 | 37 | ---------- |
33 | 38 | hash_func : callable, optional |
@@ -367,6 +372,51 @@ def delete_stale_entries(self, stale_after: timedelta) -> None: |
367 | 372 | warnings.warn(f"S3 delete_stale_entries failed: {exc}", stacklevel=2) |
368 | 373 |
|
369 | 374 | # ------------------------------------------------------------------ |
370 | | - # Async variants delegate to the thread-based defaults in _BaseCore |
371 | | - # since boto3 is a sync library. |
| 375 | + # Async variants explicitly offload sync boto3 operations to avoid |
| 376 | + # blocking the event loop thread. |
372 | 377 | # ------------------------------------------------------------------ |
| 378 | + |
| 379 | + async def aget_entry(self, args, kwds) -> Tuple[str, Optional[CacheEntry]]: |
| 380 | + """Async-compatible variant of :meth:`get_entry`. |
| 381 | +
|
| 382 | + This method delegates to the sync implementation via |
| 383 | + ``asyncio.to_thread`` because boto3 is sync-only. |
| 384 | +
|
| 385 | + """ |
| 386 | + return await asyncio.to_thread(self.get_entry, args, kwds) |
| 387 | + |
| 388 | + async def aget_entry_by_key(self, key: str) -> Tuple[str, Optional[CacheEntry]]: |
| 389 | + """Async-compatible variant of :meth:`get_entry_by_key`. |
| 390 | +
|
| 391 | + This method delegates to the sync implementation via |
| 392 | + ``asyncio.to_thread`` because boto3 is sync-only. |
| 393 | +
|
| 394 | + """ |
| 395 | + return await asyncio.to_thread(self.get_entry_by_key, key) |
| 396 | + |
| 397 | + async def aset_entry(self, key: str, func_res: Any) -> bool: |
| 398 | + """Async-compatible variant of :meth:`set_entry`. |
| 399 | +
|
| 400 | + This method delegates to the sync implementation via |
| 401 | + ``asyncio.to_thread`` because boto3 is sync-only. |
| 402 | +
|
| 403 | + """ |
| 404 | + return await asyncio.to_thread(self.set_entry, key, func_res) |
| 405 | + |
| 406 | + async def amark_entry_being_calculated(self, key: str) -> None: |
| 407 | + """Async-compatible variant of :meth:`mark_entry_being_calculated`. |
| 408 | +
|
| 409 | + This method delegates to the sync implementation via |
| 410 | + ``asyncio.to_thread`` because boto3 is sync-only. |
| 411 | +
|
| 412 | + """ |
| 413 | + await asyncio.to_thread(self.mark_entry_being_calculated, key) |
| 414 | + |
| 415 | + async def amark_entry_not_calculated(self, key: str) -> None: |
| 416 | + """Async-compatible variant of :meth:`mark_entry_not_calculated`. |
| 417 | +
|
| 418 | + This method delegates to the sync implementation via |
| 419 | + ``asyncio.to_thread`` because boto3 is sync-only. |
| 420 | +
|
| 421 | + """ |
| 422 | + await asyncio.to_thread(self.mark_entry_not_calculated, key) |
0 commit comments