Path: csiph.com!usenet.pasdenom.info!gegeweb.org!usenet-fr.net!nerim.net!novso.com!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!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.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'algorithm': 0.03; 'finally:': 0.05; 'startup': 0.05; 'rename': 0.07; 'subject:file': 0.07; 'terminated': 0.07; 'try:': 0.07; 'subject:How': 0.09; '(although': 0.09; 'exist.': 0.09; 'violates': 0.09; 'suggest': 0.11; 'files.': 0.13; '(but': 0.15; 'cases': 0.15; 'file,': 0.15; 'deleted,': 0.16; 'flock': 0.16; 'perfect.': 0.16; 'renamed': 0.16; 'status.': 0.16; 'temp': 0.16; 'time).': 0.16; 'tmp_file': 0.16; 'writer': 0.16; 'file.': 0.20; 'written': 0.20; 'trying': 0.21; "i'd": 0.22; 'seems': 0.23; 'header:In-Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; 'select': 0.26; '(most': 0.27; 'older': 0.27; 'newer': 0.27; 'consult': 0.29; 'locks': 0.29; 'case,': 0.29; 'read,': 0.29; 'probably': 0.29; 'file': 0.32; 'not.': 0.32; 'could': 0.32; 'info': 0.32; 'to:addr:python-list': 0.33; 'that,': 0.34; 'needed': 0.35; 'richard': 0.35; 'received:192.168.0': 0.35; 'something': 0.35; 'there': 0.35; 'really': 0.36; 'but': 0.36; 'best,': 0.37; 'does': 0.37; 'two': 0.37; 'rather': 0.37; 'subject:: ': 0.38; 'delete': 0.38; 'to:addr:python.org': 0.39; 'received:192': 0.39; 'short': 0.39; 'received:192.168': 0.40; 'help': 0.40; 'your': 0.60; 'most': 0.61; 'latest': 0.61; 'received:62': 0.62; 'here': 0.65; 'jul': 0.65; 'physical': 0.69; 'race': 0.71; 'guaranteed': 0.76; 'one).': 0.84; 'received:192.168.13': 0.84; 'received:62.179': 0.84; 'received:62.179.121': 0.84; 'received:62.179.121.47': 0.84; 'received:upcmail.net': 0.84; 'subject:status': 0.84; 'received:192.168.0.100': 0.91; 'mount': 0.93 X-SourceIP: 89.134.225.226 Date: Sun, 08 Jul 2012 22:57:56 +0200 From: Laszlo Nagy User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:13.0) Gecko/20120614 Thunderbird/13.0.1 MIME-Version: 1.0 To: python-list@python.org Subject: Re: How to safely maintain a status file References: In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 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: 55 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1341781085 news.xs4all.nl 6953 [2001:888:2000:d::a6]:34678 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:25052 On Sun, 8 Jul 2012 21:29:41 +1000, Richard Baron Penman declaimed the following in gmane.comp.python.general: >> and then on startup read from tmp_file if status_file does not exist. >> But this seems awkward. >> > It also violates your requirement -- since the "crash" could take > place with a partial "temp file". > > I'd suggest that, rather than deleting the old status file, you > rename IT -- and only delete it IF you successfully rename the temp > file. Yes, this is much better. Almost perfect. Don't forget to consult your system documentation, and check if the rename operation is atomic or not. (Most probably it will only be atomic if the original and the renamed file are on the same physical partition and/or mount point). But even if the rename operation is atomic, there is still a race condition. Your program can be terminated after the original status file has been deleted, and before the temp file was renamed. In this case, you will be missing the status file (although your program already did something just it could not write out the new status). Here is an algorithm that can always write and read a status (but it might not be the latest one). You can keep the last two status files. Writer: * create temp file, write new status info * create lock file if needed * flock it * try: * delete older status file * rename temp file to new status file * finally: unlock the lock file Reader: * flock the lock file * try: * select the newer status file * read status info * finally: unlock the lock file It is guaranteed that you will always have a status to read, and in most cases this will be the last one (because the writer only locks for a short time). However, it is still questionable, because your writer may be waiting for the reader to unlock, so the new status info may not be written immediatelly. It would really help if you could tell us what are you trying to do that needs status. Best, Laszlo