Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #31645

Re: locking files on Linux

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <andrea.crotti.0@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.001
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'received:209.85.223': 0.03; 'value,': 0.03; '__name__': 0.07; 'filename': 0.07; 'type,': 0.07; "'w')": 0.09; 'here?': 0.09; 'subject:files': 0.09; 'cc:addr :python-list': 0.10; 'def': 0.10; 'file,': 0.15; "'__main__':": 0.16; '100))': 0.16; 'fail,': 0.16; 'fcntl': 0.16; 'fine.': 0.16; 'skip:p 30': 0.20; 'import': 0.21; 'modifying': 0.22; 'cc:2**0': 0.23; 'example': 0.23; "i've": 0.23; 'cc:no real name:2**0': 0.24; 'tried': 0.25; 'cc:addr:python.org': 0.25; 'header:In-Reply-To:1': 0.25; 'separate': 0.27; 'message-id:@mail.gmail.com': 0.27; 'locking': 0.29; 'url:mailman': 0.29; 'skip:_ 10': 0.29; 'class': 0.29; 'maybe': 0.29; 'expect': 0.31; 'url:python': 0.32; 'file': 0.32; 'url:listinfo': 0.32; 'instances': 0.33; 'received:google.com': 0.34; 'fail': 0.35; 'received:209.85': 0.35; 'but': 0.36; 'url:org': 0.36; 'should': 0.36; 'too': 0.36; 'problems': 0.36; 'received:209': 0.37; 'subject:: ': 0.38; 'some': 0.38; 'release': 0.39; 'application': 0.40; 'header:Received:5': 0.40; 'url:mail': 0.40; 'your': 0.60; 'time,': 0.62; 'russian': 0.71; 'edwards': 0.91; 'to:none': 0.93
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=XMvCKCK+RZPNi0NYQ84YQ0iirLtsFsJxZgWHuWS79BM=; b=yykqyEHOs9ZE+orZiEayFOph5K0NSjtYIgVIASSPrKY4ZZUKj5yjBLstdoMnVRl0k7 JmlOdr/ahfTd+SNrxfaWOO8HHMwTpm532wI80Pwd5T3ok6i9o6MKP42ovz/+6PmR+CW5 FONJXTLwf8aqH+KzNDNujL94nG6KOGZSozi3qCTKLYHcGNzW5rRx76SHHaBdz9BB5CMv +Lf8OiZGaGe6vUjwuwXgnqMvyfCM/yqFmhg/AV0DJJ5k0QlHzyJbePkQAIDzifId0VHm eyzd+k+sdR0qJy6qOUPuIawKRb2Kzy+KKWtzCdWjEFTlBEAD8oXRIEW0ZiaM4DmpeaVL k4dw==
MIME-Version 1.0
In-Reply-To <k5p1rg$et4$1@reader1.panix.com>
References <mailman.2430.1350566044.27098.python-list@python.org> <k5p03u$3t8$2@reader1.panix.com> <mailman.2431.1350567869.27098.python-list@python.org> <k5p1rg$et4$1@reader1.panix.com>
Date Thu, 18 Oct 2012 15:49:02 +0100
Subject Re: locking files on Linux
From andrea crotti <andrea.crotti.0@gmail.com>
Cc python-list@python.org
Content-Type text/plain; charset=ISO-8859-1
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.2440.1350571745.27098.python-list@python.org> (permalink)
Lines 52
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1350571745 news.xs4all.nl 6895 [2001:888:2000:d::a6]:41973
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:31645

Show key headers only | View raw


2012/10/18 Grant Edwards <invalid@invalid.invalid>:
>
> If what you're guarding against is multiple instances of your
> application modifying the file, then either of the advisory file
> locking schemes or the separate lock file should work fine.
>
> --
> Grant Edwards               grant.b.edwards        Yow! All this time I've
>                                   at               been VIEWING a RUSSIAN
>                               gmail.com            MIDGET SODOMIZE a HOUSECAT!
> --
> http://mail.python.org/mailman/listinfo/python-list

Ok so I tried a small example to see if I can make it fail, but this
below just works perfectly fine.

Maybe it's too fast and it release the file in time, but I would
expect it to take some time and fail instead..

import fcntl

from multiprocessing import Process

FILENAME = 'file.txt'


def long_text():
    return ('some text' * (100 * 100))


class Locked:
    def __init__(self, fileobj):
        self.fileobj = fileobj

    def __enter__(self):
        # any problems here?
        fcntl.lockf(self.fileobj, fcntl.LOCK_EX)
        return self.fileobj

    def __exit__(self, type, value, traceback):
        fcntl.lockf(self.fileobj, fcntl.LOCK_UN)


def write_to_file():
    with open(FILENAME, 'w') as to_lock:
        with Locked(to_lock):
            to_lock.write(long_text())


if __name__ == '__main__':
    Process(target=write_to_file).start()
    Process(target=write_to_file).start()

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

locking files on Linux andrea crotti <andrea.crotti.0@gmail.com> - 2012-10-18 14:14 +0100
  Re: locking files on Linux Grant Edwards <invalid@invalid.invalid> - 2012-10-18 13:27 +0000
    Re: locking files on Linux andrea crotti <andrea.crotti.0@gmail.com> - 2012-10-18 14:44 +0100
      Re: locking files on Linux Grant Edwards <invalid@invalid.invalid> - 2012-10-18 13:57 +0000
        Re: locking files on Linux andrea crotti <andrea.crotti.0@gmail.com> - 2012-10-18 15:49 +0100
        Re: locking files on Linux Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2012-10-18 16:04 +0100
        Re: locking files on Linux andrea crotti <andrea.crotti.0@gmail.com> - 2012-10-18 16:08 +0100
        Re: locking files on Linux Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2012-10-18 16:31 +0100
        Re: locking files on Linux andrea crotti <andrea.crotti.0@gmail.com> - 2012-10-19 13:15 +0100
      Re: locking files on Linux Nobody <nobody@nowhere.com> - 2012-10-19 15:36 +0100
    Re: locking files on Linux Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2012-10-18 14:58 +0100

csiph-web