4 ms·
One application of this could be writing to a SQLite database. If you wrap your writes in a shell script controlled by a lock directory, you are guaranteed to
by chasil 3y ago
One application of this could be writing to a SQLite database.
If you wrap your writes in a shell script controlled by a lock directory, you are guaranteed to never receive SQLITE_BUSY.
#!/bin/sh
until mkdir /var/tmp/mylock
do sleep 1;
done
trap 'rmdir /var/tmp/mylock' ABRT EXIT FPE HUP INT QUIT SEGV TERM
sqlite3 mydatabase.db 'INSERT INTO MYTABLE VALUES (1,2,3);'
Since the mkdir() is atomic, the SQLite busy handler is now irrelevant with this approach.
On Linux, there is also an flock utility, but it tends to need subshells which are not as efficient.
EDIT: This does reduce the write performance to one of these executions per second. Simple inserts like this can hit a few dozen per second otherwise, as I have heard.