Prev: Sharing a program I wrote
Next: Exclusively lock a file to prevent other processes from reading it?
From: Philip Semanchuk on 4 May 2010 17:46 On May 4, 2010, at 5:37 PM, python(a)bdurham.com wrote: > Is there a way to exclusively lock a file to prevent other > processes from reading it while we have it open? > > I need to cache some overflow data to disk in a temp file and I > want to make sure no other processes can read the contents of > this file while I'm using it. > > I tried the following using an 'append binary' mode, but I can > use NotePad to read the file while I'm using it: > > fd = open( r'a-temp-file.dat', 'ab' ) > > My environment is Python 2.6.4 (32-bit) under Windows, but I'm > looking for a cross-platform solution if that's possible. Have a look at tempfile.mkstemp() in the standard library.
From: Christian Heimes on 4 May 2010 18:38 python(a)bdurham.com wrote: > Is there a way to exclusively lock a file to prevent other > processes from reading it while we have it open? > > I need to cache some overflow data to disk in a temp file and I > want to make sure no other processes can read the contents of > this file while I'm using it. > > I tried the following using an 'append binary' mode, but I can > use NotePad to read the file while I'm using it: > > fd = open( r'a-temp-file.dat', 'ab' ) > > My environment is Python 2.6.4 (32-bit) under Windows, but I'm > looking for a cross-platform solution if that's possible. I know of now mandatory file lock solution that works cross platform. On Unix file locks (flock(2)) are advisory locks. All applications must check the file lock and act accordantly. Your best choice is other a temporary file or a temporary directory owned by your application. Python's tempfile module contains several solutions to securely work with temporary files and directories. Please don't try to come up with your own solution. Your app can easily become vulnerable by symlink attacks. Christian
From: John Nagle on 5 May 2010 00:19 Philip Semanchuk wrote: > > On May 4, 2010, at 5:37 PM, python(a)bdurham.com wrote: > >> Is there a way to exclusively lock a file to prevent other >> processes from reading it while we have it open? If you can use SQLite to store the data, it will deal with your locking problems. The pain of getting locking right has already been dealt with by others. Classic lock files are iffy. They're not race condition free on NTFS, and all the junk needed to clean up lock files properly after a crash is a headache. John Nagle
|
Pages: 1 Prev: Sharing a program I wrote Next: Exclusively lock a file to prevent other processes from reading it? |