diff options
author | jsemric <jakubsemric@gmail.com> | 2021-04-21 13:55:47 +0200 |
---|---|---|
committer | jsemric <jakubsemric@gmail.com> | 2021-04-21 13:55:47 +0200 |
commit | 8c6728341a4020200c1b150b2e8f0804ae3c3714 (patch) | |
tree | 69a67e7cc772087d741dd17b2bf569eebdec0b74 | |
parent | 8b3c6deb833b9ab516b14e4ddfd86250382ef6e8 (diff) | |
download | requests-cache-8c6728341a4020200c1b150b2e8f0804ae3c3714.tar.gz |
remove locking of sqlite connection
-rw-r--r-- | requests_cache/backends/sqlite.py | 28 |
1 files changed, 10 insertions, 18 deletions
diff --git a/requests_cache/backends/sqlite.py b/requests_cache/backends/sqlite.py index 97a954b..8cc301b 100644 --- a/requests_cache/backends/sqlite.py +++ b/requests_cache/backends/sqlite.py @@ -65,28 +65,20 @@ class DbDict(BaseStorage): self.table_name = table_name self._can_commit = True - self._lock = threading.RLock() self._local_context = threading.local() with self.connection() as con: con.execute("create table if not exists `%s` (key PRIMARY KEY, value)" % self.table_name) @contextmanager - 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) - if self.fast_save: - self._local_context.con.execute("PRAGMA synchronous = 0;") - yield self._local_context.con - 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 connection(self, commit_on_success=False): + 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) + if self.fast_save: + self._local_context.con.execute("PRAGMA synchronous = 0;") + yield self._local_context.con + if commit_on_success and self._can_commit: + self._local_context.con.commit() def __del__(self): if hasattr(self._local_context, "con"): @@ -113,7 +105,7 @@ class DbDict(BaseStorage): self._can_commit = True def __getitem__(self, key): - with self.connection(lock=False) as con: + with self.connection() 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: |