sqlitedict icon indicating copy to clipboard operation
sqlitedict copied to clipboard

Multi thread example

Open sionking opened this issue 7 years ago • 2 comments
trafficstars

I trigger 10 thread of workers that fill the userDb, without any locking mechanisem

def worker(name, value):
    userDb[name] = value

def sync():
    userDb.commit()

userDb = SqliteDict('./usernames.sqlite', encode=json.dumps, decode=json.loads, autocommit=False)

I also triger sync thread every 5 minutes, is it safe to do so ?

sionking avatar Nov 14 '18 07:11 sionking

@sionking not really sure that this is safe, @piskvorky?

menshikh-iv avatar Jan 17 '19 04:01 menshikh-iv

I'd say yes.

More concretely, it's safe to the degree Sqlite itself supports these operations and workload. All SqliteDict does is serialize the requests from multiple threads into a single queue, executing them one after the other. It adds thread safety, not extra performance.

piskvorky avatar Jan 17 '19 07:01 piskvorky

@piskvorky Im guessing the order of the queue is somewhat unpredictable? If I have 10 threads all writing to the same key (and say some of them happen to write at the exact same time) I don't know the order of which they will be executed?

leahcornelius avatar Dec 23 '22 08:12 leahcornelius

Correct.

piskvorky avatar Dec 23 '22 11:12 piskvorky