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


Groups > comp.lang.python > #25044

Re: How to safely maintain a status file

Path csiph.com!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!newsfeed.eweka.nl!eweka.nl!feeder3.eweka.nl!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <mhrivnak@gmail.com>
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; 'cc:addr:python-list': 0.10; 'def': 0.10; 'thread': 0.11; 'suggest': 0.11; 'b):': 0.16; 'disk.': 0.16; 'renames': 0.16; 'storing': 0.16; 'suppport': 0.16; 'tmpfile': 0.16; 'wrote:': 0.17; 'sender:addr:gmail.com': 0.18; 'windows': 0.19; 'exists.': 0.22; 'received:mail-bk0-f46.google.com': 0.22; 'cc:2**0': 0.23; 'work.': 0.23; 'raise': 0.24; 'cc:no real name:2**0': 0.24; 'linux': 0.24; 'cc:addr:python.org': 0.25; 'header:In-Reply-To:1': 0.25; 'am,': 0.27; 'disk': 0.27; 'received:209.85.214.46': 0.27; 'message-id:@mail.gmail.com': 0.27; "doesn't": 0.28; 'initial': 0.28; 'url:mailman': 0.29; "skip:' 10": 0.30; 'writes': 0.30; 'error': 0.30; 'code': 0.31; 'implement': 0.32; 'url:python': 0.32; 'file': 0.32; 'url:listinfo': 0.32; 'received:google.com': 0.34; 'christian': 0.34; 'richard': 0.35; 'saved': 0.35; 'received:209.85': 0.35; 'there': 0.35; 'except': 0.36; 'michael': 0.36; 'but': 0.36; 'url:org': 0.36; 'url:library': 0.36; 'two': 0.37; 'received:209': 0.37; 'data': 0.37; 'subject:: ': 0.38; 'store': 0.38; 'skip:o 20': 0.38; 'url:docs': 0.38; 'several': 0.39; 'received:209.85.214': 0.39; 'header:Received:5': 0.40; 'url:mail': 0.40; 'first': 0.61; 'side': 0.61; 'times': 0.63; 'jul': 0.65; 'finally': 0.66; 'subject:status': 0.84
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:sender:in-reply-to:references:date :x-google-sender-auth:message-id:subject:from:to:cc:content-type; bh=bDso9U+tv+/q5S2UxwbHvPkghtD1Z8AujJFy6+9T07Q=; b=fiNgPKOsgnfYq7SYNmPwj9IXwSfQH8poyN48hkVnQymCufzjzDynLy3OEQaM8ItYKs 9BH2ITlkTcJUOQ8MfLqD+lJxOt17py1aORTrJnnC+spNZExa09FD67z31TRCc506Ia7y m1I2jteLfSlcNTiGsvuwEB2YN865mFHlm3O1Xouga/6qDMrf2TCHI42vz8DvSGAQIve7 LvL6+eco4IjwMnm42AXm0A/TO84rYFufX2fUxG8IdqlnguDBHicqCDOx++vg4Zn0VVMD gyeLMTTBHJeFduQ0taD5wQMj2wJK7Lm4syJtcyC+XbfUiJGQtXTA/oZBVvjUpJfwd8av NSNQ==
MIME-Version 1.0
Sender mhrivnak@gmail.com
In-Reply-To <jtbsb1$8fj$1@dough.gmane.org>
References <CAOV1wRVtm27yWez1HZuN8=ia-TyM2aXp9QCUbSZ5aZExP_ZChA@mail.gmail.com> <jtbsb1$8fj$1@dough.gmane.org>
Date Sun, 8 Jul 2012 12:47:25 -0400
X-Google-Sender-Auth 0tCZJuYnh9tUN6jhGRrqtuo_A5A
Subject Re: How to safely maintain a status file
From Michael Hrivnak <mhrivnak@hrivnak.org>
To Christian Heimes <lists@cheimes.de>
Content-Type text/plain; charset=ISO-8859-1
Cc python-list@python.org
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.1920.1341766046.4697.python-list@python.org> (permalink)
Lines 59
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1341766046 news.xs4all.nl 6907 [2001:888:2000:d::a6]:57381
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:25044

Show key headers only | View raw


What are you keeping in this status file that needs to be saved
several times per second?  Depending on what type of state you're
storing and how persistent it needs to be, there may be a better way
to store it.

Michael

On Sun, Jul 8, 2012 at 7:53 AM, Christian Heimes <lists@cheimes.de> wrote:
> 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
>
> --
> http://mail.python.org/mailman/listinfo/python-list

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


Thread

Re: How to safely maintain a status file Michael Hrivnak <mhrivnak@hrivnak.org> - 2012-07-08 12:47 -0400
  Re: How to safely maintain a status file Plumo <richardbp@gmail.com> - 2012-07-08 22:52 -0700
    Re: How to safely maintain a status file Michael Hrivnak <mhrivnak@hrivnak.org> - 2012-07-09 16:47 -0400

csiph-web