FAQ
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.

Thanks,
Malcolm
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20100504/251ff109/attachment-0001.html>

Search Discussions

  • Philip Semanchuk at May 4, 2010 at 9:46 pm

    On May 4, 2010, at 5:37 PM, python at 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.
  • Chris Rebert at May 4, 2010 at 9:48 pm

    On Tue, May 4, 2010 at 2:37 PM, 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 remember this recipe (or a variant) from the dead-tree Python Cookbook:
    http://code.activestate.com/recipes/65203-portalocker-cross-platform-posixnt-api-for-flock-s/

    Cheers,
    Chris
  • Christian Heimes at May 4, 2010 at 10:38 pm

    python at 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
  • Nobody at May 5, 2010 at 12:03 am

    On May 4, 2010, at 5:37 PM, python at bdurham.com wrote:

    Is there a way to exclusively lock a file to prevent other processes
    from reading it while we have it open?
    My environment is Python 2.6.4 (32-bit) under Windows, but I'm looking
    for a cross-platform solution if that's possible.
    Some Unix systems support mandatory locking; Linux used to, but the option
    appears to have been removed (and was seldom enabled).

    On Unix systems which support mandatory locking, it is enabled for a
    particular file by setting the setgid bit on a file which lacks
    group-execute permission. This causes fcntl() locks to be mandatory, i.e.
    they cause read()/write() operations to block rather than merely
    preventing conflicting locks from being obtained.

    But in general, you can't rely upon mandatory locking being available.
  • John Nagle at May 5, 2010 at 4:19 am

    Philip Semanchuk wrote:
    On May 4, 2010, at 5:37 PM, python at 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
  • Python at May 5, 2010 at 1:20 pm
    Chris, Philip, Christian, John and others,

    Thank you all for your replies.

    Regards,
    Malcolm
    -------------- next part --------------
    An HTML attachment was scrubbed...
    URL: <http://mail.python.org/pipermail/python-list/attachments/20100505/ea69decc/attachment.html>

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedMay 4, '10 at 9:37p
activeMay 5, '10 at 1:20p
posts7
users6
websitepython.org

People

Translate

site design / logo © 2023 Grokbase