Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!xlned.com!feeder7.xlned.com!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: 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; 'value,': 0.03; '__name__': 0.07; 'filename': 0.07; 'type,': 0.07; "'w')": 0.09; '(without': 0.09; 'here?': 0.09; 'subject:files': 0.09; 'suggestions:': 0.09; 'truncate': 0.09; 'cc:addr:python-list': 0.10; 'def': 0.10; 'file,': 0.15; "'__main__':": 0.16; '*before*': 0.16; '100))': 0.16; 'fail,': 0.16; 'fcntl': 0.16; 'fine.': 0.16; 'wrote:': 0.17; 'skip:p 30': 0.20; 'import': 0.21; 'modifying': 0.22; 'cc:2**0': 0.23; 'example': 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; 'checking': 0.27; 'separate': 0.27; 'message-id:@mail.gmail.com': 0.27; 'received:209.85.212': 0.28; 'locking': 0.29; 'skip:_ 10': 0.29; 'class': 0.29; 'this.': 0.29; 'maybe': 0.29; 'expect': 0.31; 'file': 0.32; 'instances': 0.33; 'received:google.com': 0.34; 'fail': 0.35; 'received:209.85': 0.35; 'but': 0.36; 'test': 0.36; 'should': 0.36; 'too': 0.36; 'problems': 0.36; 'october': 0.37; 'two': 0.37; 'why': 0.37; 'received:209': 0.37; 'data': 0.37; 'subject:: ': 0.38; 'some': 0.38; 'shows': 0.38; 'release': 0.39; 'application': 0.40; 'header:Received:5': 0.40; 'end': 0.40; 'think': 0.40; 'your': 0.60; 'time,': 0.62; 'different': 0.63; 'here': 0.65; 'andrea': 0.84; 'oscar': 0.84; 'edwards': 0.91 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:to :cc:content-type; bh=MwGum4APVer7zY1Sg/CjYSiU/K5vSFCYunluShnNQKU=; b=nDU+cetV4N8ytOGCHovjpezYIqzbfw/XNPSj0cse8Dbr1iWukF/qYNbgYe4msYEtLd 0eyIGwjQhZ2x0Rd78y+aXaMeFy7ycZjfGxaSI8U3XUkpFdFS+1zrhZOCH8A8Clka236e cWwC+UG/lC2TZV9hlyfyV+Mi6qnmxDbffZR49kJ6v2aNcxrD2dlOyt7Yqf5irstCupoH JMjxdWFklcGIDo9IyLmI/hExqdDZSaowtfZLiS8tdTC9sBQClhCm1I/Gk2ZLrK+4CKRM 1XKkP4Z09ok+kUyruemmedjk8EbVYvD8mAykzioOKTebJCTSxbQI4bgUGw1Ktklro0Gi 6ffg== MIME-Version: 1.0 In-Reply-To: References: Date: Thu, 18 Oct 2012 16:04:43 +0100 Subject: Re: locking files on Linux From: Oscar Benjamin To: andrea crotti Content-Type: text/plain; charset=ISO-8859-1 Cc: python-list@python.org X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 62 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1350572684 news.xs4all.nl 6935 [2001:888:2000:d::a6]:51291 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:31649 On 18 October 2012 15:49, andrea crotti wrote: > 2012/10/18 Grant Edwards : >> >> 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. > > 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.. Why not come up with a test that actually shows you if it works? Here are two suggestions: 1) Use time.sleep() so that you know how long the lock is held for. 2) Write different data into the file from each process and see what you end up with. > > 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: I don't think it will work if you truncate the file like this. This will empty the file *before* checking for the lock. Try opening the file for reading and writing (without truncating). > with Locked(to_lock): > to_lock.write(long_text()) > > > if __name__ == '__main__': > Process(target=write_to_file).start() > Process(target=write_to_file).start() Oscar