write to the same file from multiple processes at the same time?

Jp Calderone exarkun at divmod.com
Fri May 27 10:17:58 EDT 2005


On 27 May 2005 06:43:04 -0700, Paul Rubin <"http://phr.cx"@nospam.invalid> wrote:
>Jp Calderone <exarkun at divmod.com> writes:
>> >But they haven't.  They depend on messy things like server processes
>> >constantly running, which goes against the idea of a cgi that only
>> >runs when someone calls it.
>>
>> SQLite is an in-process dbm.
>
>http://www.sqlite.org/faq.html#q7
>
>    (7) Can multiple applications or multiple instances of the same
>    application access a single database file at the same time?
>
>    Multiple processes can have the same database open at the same
>    time. Multiple processes can be doing a SELECT at the same
>    time. But only one process can be making changes to the database
>    at once.
>
>But multiple processes changing the database simultaneously is
>precisely what the OP wants to do.

Er, no.  The OP precisely wants exactly one process to be able to write at a time.  If he was happy with multiple processes writing simultaneously, he wouldn't need any locking mechanism at all >:)

If you keep reading that FAQ entry, you discover that SQLite implements its own locking mechanism internally, allowing different processes to *interleave* writes to the database, and preventing any data corruption which might arise from simultaneous writes.

That said, I think an RDBM is a ridiculously complex solution to this simple problem.  A filesystem lock, preferably using the directory or symlink trick (but flock() is fun too, if you're into that sort of thing), is clearly the solution to go with here.

Jp



More information about the Python-list mailing list