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: 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: References: Date: Thu, 18 Oct 2012 15:49:02 +0100 Subject: Re: locking files on Linux From: andrea crotti 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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: 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 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. > > -- > 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()