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


Groups > comp.lang.python > #25043

Re: How to safely maintain a status file

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!ecngs!feeder2.ecngs.de!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'algorithm': 0.03; 'finally:': 0.05; 'raises': 0.07; 'rename': 0.07; 'subject:file': 0.07; 'tmp': 0.07; 'try:': 0.07; 'subject:How': 0.09; '"w")': 0.09; 'directory)': 0.09; 'e.errno': 0.09; 'flush': 0.09; 'os.name': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'def': 0.10; 'thread': 0.11; 'suggest': 0.11; 'b):': 0.16; 'disk.': 0.16; 'from:addr:cheimes.de': 0.16; 'from:addr:lists': 0.16; 'from:name:christian heimes': 0.16; 'message-id:@dough.gmane.org': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'renames': 0.16; 'suppport': 0.16; 'tmpfile': 0.16; 'windows': 0.19; 'exists.': 0.22; 'work.': 0.23; 'raise': 0.24; 'linux': 0.24; 'header:In-Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; 'disk': 0.27; "doesn't": 0.28; 'header:X-Complaints-To:1': 0.28; 'initial': 0.28; "skip:' 10": 0.30; 'writes': 0.30; 'error': 0.30; 'code': 0.31; 'implement': 0.32; 'url:python': 0.32; 'file': 0.32; 'to:addr:python-list': 0.33; 'christian': 0.34; 'richard': 0.35; 'received:org': 0.36; 'except': 0.36; 'but': 0.36; 'url:org': 0.36; 'url:library': 0.36; 'two': 0.37; 'data': 0.37; 'subject:: ': 0.38; 'store': 0.38; 'skip:o 20': 0.38; 'url:docs': 0.38; 'to:addr:python.org': 0.39; 'header:Received:5': 0.40; 'first': 0.61; 'side': 0.61; 'finally': 0.66; 'subject:status': 0.84
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Christian Heimes <lists@cheimes.de>
Subject Re: How to safely maintain a status file
Date Sun, 08 Jul 2012 13:53:05 +0200
References <CAOV1wRVtm27yWez1HZuN8=ia-TyM2aXp9QCUbSZ5aZExP_ZChA@mail.gmail.com>
Mime-Version 1.0
Content-Type text/plain; charset=ISO-8859-1
Content-Transfer-Encoding 7bit
X-Gmane-NNTP-Posting-Host f049225023.adsl.alicedsl.de
User-Agent Mozilla/5.0 (X11; Linux x86_64; rv:12.0) Gecko/20120430 Thunderbird/12.0.1
In-Reply-To <CAOV1wRVtm27yWez1HZuN8=ia-TyM2aXp9QCUbSZ5aZExP_ZChA@mail.gmail.com>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
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.1917.1341748397.4697.python-list@python.org> (permalink)
Lines 49
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1341748398 news.xs4all.nl 6929 [2001:888:2000:d::a6]:36417
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:25043

Show key headers only | View raw


Am 08.07.2012 13:29, schrieb Richard Baron Penman:
> My initial solution was a thread that writes status to a tmp file
> first and then renames:
> 
> open(tmp_file, 'w').write(status)
> os.rename(tmp_file, status_file)

You algorithm may not write and flush all data to disk. You need to do
additional work. You must also store the tmpfile on the same partition
(better: same directory) as the status file

with open(tmp_file, "w") as f:
    f.write(status)
    # flush buffer and write data/metadata to disk
    f.flush()
    os.fsync(f.fileno())

# now rename the file
os.rename(tmp_file, status_file)

# finally flush metadata of directory to disk
dirfd = os.open(os.path.dirname(status_file), os.O_RDONLY)
try:
    os.fsync(dirfd)
finally:
    os.close(dirfd)


> This works well on Linux but Windows raises an error when status_file
> already exists.
> http://docs.python.org/library/os.html#os.rename

Windows doesn't suppport atomic renames if the right side exists.  I
suggest that you implement two code paths:

if os.name == "posix":
    rename = os.rename
else:
    def rename(a, b):
        try:
            os.rename(a, b)
        except OSError, e:
            if e.errno != 183:
                raise
            os.unlink(b)
            os.rename(a, b)

Christian

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


Thread

Re: How to safely maintain a status file Christian Heimes <lists@cheimes.de> - 2012-07-08 13:53 +0200
  Re: How to safely maintain a status file Plumo <richardbp@gmail.com> - 2012-07-08 22:50 -0700
    Re: How to safely maintain a status file Christian Heimes <lists@cheimes.de> - 2012-07-09 10:17 +0200
    Re: How to safely maintain a status file Laszlo Nagy <gandalf@shopzeus.com> - 2012-07-12 19:46 +0200

csiph-web