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


Groups > comp.lang.python > #31645

Re: locking files on Linux

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 2012-10-18 15:49 +0100
Subject Re: locking files on Linux
From andrea crotti <andrea.crotti.0@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.2440.1350571745.27098.python-list@python.org> (permalink)

Show all headers | 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