piskvorky / sqlitedict

Persistent dict, backed by sqlite3 and pickle, multithread-safe.
Apache License 2.0
1.17k stars 131 forks source link

Context manager operation thread-safe? #145

Closed teetone closed 2 years ago

teetone commented 2 years ago

Hello, I was looking through the documentation and was wondering if the following code was thread-safe without any additional locks:

from sqlitedict import SqliteDict

with SqliteDict(self.cache_path) as cache_dict:
     cache_dict[key] = response
     cache_dict.commit()
piskvorky commented 2 years ago

The code inside the context manager (the with block = one SqliteDict database connection) is thread-safe.

But opening multiple connections (multiple with blocks concurrently) – not so much. The underlying database engine, SQLite, offers only limited support for this, see its FAQ. Definitely not in scope for sqlitedict, which handles thread-safety within a single process, a single SqliteDict object = database connection.

If you need to coordinate read/write access across multiple processes, an embedded DB like SQLite is the wrong tool.