Comments (7)
I would like to know as well please.
from tinydb.
It would be great to have info on whether tiny supports concurrent reads and writes. This is not clear from the README.
Unfortunately, I believe it's not possible after viewing the source code.
Concurrent writing/reading almost certainly leads to data corruption.
And I think this piece of info is already presented in the docs.
from tinydb.
from tinydb.
You always can add your own locks (e.g. using Python's locks) to ensure that concurrent writing/reading within the same Python process works correctly. If you have multiple programs, you can add some form of file locking (see e.g. https://stackoverflow.com/questions/489861/locking-a-file-in-python) to tell a process that the file is currently in use.
In general, TinyDB doesn't make any assumptions about the data storage mechanism (due to the ability to drop in your own data storage class), so there is no generic locking in TinyDB (because file-based locking might e.g. not work on network file systems or something like S3). Leaving this to the user is the most flexible solution to me even though it requires some work from users who just use the default JSONStorage
.
from tinydb.
documentation update needed:
class tinydb.middlewares.ConcurrencyMiddleware(storage_cls)
Makes TinyDB working with multithreading.
Uses a lock so write/read operations are virtually atomic.
from tinydb.
class tinydb.middlewares.ConcurrencyMiddleware(storage_cls)
Makes TinyDB working with multithreading.
Uses a lock so write/read operations are virtually atomic.
Actually, the ConcurrencyMiddleware
has been removed in TinyDB 2.0.0 due to an incorrect implementation (#18).
from tinydb.
I am using TinyDB in a project right now and came across this problem.
Since I'm already using Redis for synchronizing some application states in my cluster, I found it easiest to reuse it for distributed locking.
My application uses a Storage class that's pretty much a copy of JSONStorage with some personal tweaks like automatic backups and custom caching.
In the custom storage's __init__()
definition I added this (slightly modified):
...
self._access_id = kwargs.pop("access_id")
try:
if r.get("DATABASE_LOCK") != self._access_id:
while not r.set(
"DATABASE_LOCK",
self._access_id,
px=4000, # Max lock time in ms
nx=True,
):
continue
except Exception as e:
r.delete("DATABASE_LOCK")
raise
...
The close
method looks like this:
...
self._handle.close()
r.delete("DATABASE_LOCK")
...
TinyDB is used like this:
TINYDB = {
"storage": JSONStorageLocked,
"path": "database/data.json",
"access_id": str(uuid4()),
}
with TinyDB(**defaults.TINYDB) as db:
...
Since the storage instance is reinitialized during operations, I was not able to use id(self) but had to define a fixed ID to use inside the whole context.
At first I tried to only hold a lock inside the read
and write
definitions, but in stress tests it of course failed sometimes. The application would lock for a data read, unlock, lock for a write and append the data. But between read and write there is a tiny window for another worker to lock and write data, so I dropped that idea.
Of course I could switch back to that logic and check for the last writer's ID, but I'm fine with it now. :) I also use some caching in Redis, so it does not really impact the performance anyway for the few writes I'm doing.
from tinydb.
Related Issues (20)
- Adding query limit 😄
- Empty table does not persist HOT 1
- Modifying documents returned by `search` modifies the query cache
- Can't use lists/dicts as arguments to `Query.test`
- how to flush Tables with CachingMiddleware(JSONStorage)) HOT 2
- de-serialization Query() statement for search is there a way not use eval HOT 3
- Data Corruption vulnerability on TinyDB version 4.7.1 HOT 5
- Does tinyDB support multi-thread/process access ? HOT 2
- TinyDB 4.8.0 issue HOT 2
- -1073741571 (0xC00000FD) something interesting occured. HOT 2
- Are there any option to persist query caches? HOT 1
- Json decoder error when reading DB from multiple processes HOT 2
- Broken link in documentation.
- is there any way to speed up deletion HOT 1
- Document ID and existing unique value in the document HOT 1
- Search documents that do not have a specific key
- A simple implementation for update_multiple_by_id
- bug: MemoryStorage incorrectly keeps references to nested dicts
- json context manager
- How can we ensure uniqeness when inserting HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from tinydb.