Skip to content

Commit

Permalink
Create diskcache instance in sub-thread
Browse files Browse the repository at this point in the history
  • Loading branch information
layday committed Apr 25, 2024
1 parent 2ab8e9b commit ce77810
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/instawow/http/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ async def init_web_client(
expire_after=_DEFAULT_EXPIRE,
include_headers=True,
)
with make_cache(cache_dir) as cache:
async with make_cache(cache_dir) as cache:
cache_backend.responses = cache_backend.redirects = cache
if no_cache:
cache_backend.disabled = True
Expand Down
18 changes: 11 additions & 7 deletions src/instawow/http/_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,9 @@
_P = ParamSpec('_P')


@contextlib.contextmanager
def make_cache(cache_dir: os.PathLike[str]):
with (
concurrent.futures.ThreadPoolExecutor(1, '_http_cache') as executor,
diskcache.Cache(os.fspath(cache_dir)) as cache,
):
@contextlib.asynccontextmanager
async def make_cache(cache_dir: os.PathLike[str]):
with concurrent.futures.ThreadPoolExecutor(1, '_http_cache') as executor:
loop = asyncio.get_running_loop()

def run_in_thread2(fn: Callable[_P, _U]) -> Callable[_P, Coroutine[Any, Any, _U]]:
Expand All @@ -34,6 +31,10 @@ async def wrapper(*args: _P.args, **kwargs: _P.kwargs):

return wrapper

# diskcache will only close the sqlite connection if it was initialised
# in the same thread.
cache = await run_in_thread2(diskcache.Cache)(os.fspath(cache_dir))

class Cache(aiohttp_client_cache.BaseCache):
@run_in_thread2
def bulk_delete(self, keys: Set[str]):
Expand Down Expand Up @@ -75,4 +76,7 @@ async def values(self):
def write(self, key: str, item: aiohttp_client_cache.ResponseOrKey):
cache[key] = item

yield Cache()
try:
yield Cache()
finally:
await run_in_thread2(cache.close)()

0 comments on commit ce77810

Please sign in to comment.