Skip to content

Commit

Permalink
Lazy-load asyncio.Lock in SQLiteBackend to prevent premature initiali…
Browse files Browse the repository at this point in the history
…zation outside main event loop
  • Loading branch information
JWCook committed Jun 24, 2021
1 parent d72d204 commit 690a2fe
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 3 deletions.
3 changes: 3 additions & 0 deletions HISTORY.md
@@ -1,5 +1,8 @@
# History

## 0.4.1 (2021-06-TBD)
* Fix initialziation of `SQLiteBackend` so it can be created outside main event loop

## 0.4.0 (2021-05-12)
* Add optional support for the following **request** headers:
* `Cache-Control: max-age`
Expand Down
2 changes: 1 addition & 1 deletion aiohttp_client_cache/__init__.py
@@ -1,4 +1,4 @@
__version__ = '0.4.0'
__version__ = '0.4.1'

# flake8: noqa: F401, F403
try:
Expand Down
10 changes: 8 additions & 2 deletions aiohttp_client_cache/backends/sqlite.py
Expand Up @@ -61,11 +61,11 @@ def __init__(self, filename: str, table_name: str, use_temp: bool = False, **kwa
self._bulk_commit = False
self._initialized = False
self._connection = None
self._lock = asyncio.Lock()
self._lock = None

@asynccontextmanager
async def get_connection(self, autocommit: bool = False) -> AsyncIterator[aiosqlite.Connection]:
async with self._lock:
async with self.lock:
db = (
self._connection
if self._connection
Expand All @@ -79,6 +79,12 @@ async def get_connection(self, autocommit: bool = False) -> AsyncIterator[aiosql
if not self._bulk_commit:
await db.close()

@property
def lock(self):
if self._lock is None:
self._lock = asyncio.Lock()
return self._lock

async def _init_db(self, db: aiosqlite.Connection):
"""Create table if this is the first connection opened, and set fast save if possible"""
if not self._bulk_commit:
Expand Down

0 comments on commit 690a2fe

Please sign in to comment.