diff options
| author | jsemric <jakubsemric@gmail.com> | 2021-04-21 09:33:10 +0200 |
|---|---|---|
| committer | jsemric <jakubsemric@gmail.com> | 2021-04-21 13:35:12 +0200 |
| commit | 8b3c6deb833b9ab516b14e4ddfd86250382ef6e8 (patch) | |
| tree | 38f99ae718cd73d209cb199414cd7a53f9a57054 | |
| parent | 8d558952729269814bc8cf03337bce429c66338a (diff) | |
| download | requests-cache-8b3c6deb833b9ab516b14e4ddfd86250382ef6e8.tar.gz | |
do not lock connection on read
| -rw-r--r-- | requests_cache/backends/sqlite.py | 14 |
1 files changed, 10 insertions, 4 deletions
diff --git a/requests_cache/backends/sqlite.py b/requests_cache/backends/sqlite.py index 29eee83..97a954b 100644 --- a/requests_cache/backends/sqlite.py +++ b/requests_cache/backends/sqlite.py @@ -71,8 +71,8 @@ class DbDict(BaseStorage): con.execute("create table if not exists `%s` (key PRIMARY KEY, value)" % self.table_name) @contextmanager - def connection(self, commit_on_success=False): - with self._lock: + def connection(self, commit_on_success=False, lock=True): + def _get_conn(): if not hasattr(self._local_context, "con"): logger.debug(f'Opening connection to {self.db_path}:{self.table_name}') self._local_context.con = sqlite3.connect(self.db_path, **self.connection_kwargs) @@ -82,9 +82,15 @@ class DbDict(BaseStorage): if commit_on_success and self._can_commit: self._local_context.con.commit() + if lock: + with self._lock: + yield from _get_conn() + else: + yield from _get_conn() + def __del__(self): if hasattr(self._local_context, "con"): - self._local_context.close() + self._local_context.con.close() @contextmanager def bulk_commit(self): @@ -107,7 +113,7 @@ class DbDict(BaseStorage): self._can_commit = True def __getitem__(self, key): - with self.connection() as con: + with self.connection(lock=False) as con: row = con.execute("select value from `%s` where key=?" % self.table_name, (key,)).fetchone() # raise error after the with block, otherwise the connection will be locked if not row: |
