Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #80007
| 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.001 |
| X-Spam-Evidence | '*H*': 1.00; '*S*': 0.00; 'win32': 0.03; 'rename': 0.07; 'subject:file': 0.07; 'tries': 0.07; '[1]:': 0.09; 'creation,': 0.09; 'e.g.,': 0.09; 'exits': 0.09; 'modulo': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'to)': 0.09; 'try:': 0.09; 'url:github': 0.09; 'subject:How': 0.10; 'python': 0.11; 'def': 0.12; 'times,': 0.14; '"in': 0.16; "'w')": 0.16; 'between.': 0.16; 'created.': 0.16; 'exception:': 0.16; 'initially,': 0.16; 'operation.': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'saved.': 0.16; 'skipped.': 0.16; 'true:': 0.16; 'url:py': 0.16; '(not': 0.18; 'trying': 0.19; 'replacing': 0.19; 'written,': 0.19; 'solution.': 0.20; 'import': 0.22; 'previously': 0.22; 'creating': 0.23; 'header:User-Agent:1': 0.23; 'documented': 0.24; 'replace': 0.24; 'task': 0.26; 'header:X-Complaints-To:1': 0.27; '[1]': 0.29; 'thus': 0.29; 'exceptions': 0.31; 'file:': 0.31; 'occurs': 0.31; 'writes:': 0.31; 'file': 0.32; 'running': 0.33; '(e.g.': 0.33; 'cases': 0.33; 'period': 0.33; 'skip:t 40': 0.33; 'subject:the': 0.34; 'problem': 0.35; 'except': 0.35; 'but': 0.35; 'there': 0.35; 'disk': 0.36; 'method': 0.36; 'subject:?': 0.36; 'should': 0.36; 'seconds': 0.37; 'step': 0.37; 'being': 0.38; 'message- id:@gmail.com': 0.38; 'handle': 0.38; 'to:addr:python-list': 0.38; 'previous': 0.38; 'sure': 0.39; 'to:addr:python.org': 0.39; 'either': 0.39; 'received:org': 0.40; 'according': 0.40; 'how': 0.40; 'solve': 0.60; 'new': 0.61; 'simple': 0.61; 'save': 0.62; 'such': 0.63; 'more': 0.64; 'temporary': 0.65; 'periodically': 0.68; 'saving': 0.69; 'obvious': 0.74; 'overcome': 0.74; 'power': 0.76; 'restore': 0.78; 'as:': 0.81; '10mb': 0.84; 'it"': 0.84; 'locked': 0.84; 'partial': 0.84; 'preventing': 0.84; 'url:master': 0.84; 'received:89': 0.85; 'opens': 0.91 |
| X-Injected-Via-Gmane | http://gmane.org/ |
| To | python-list@python.org |
| From | Akira Li <4kir4.1i@gmail.com> |
| Subject | Re: Saving a file "in the background" -- How? |
| Date | Fri, 31 Oct 2014 15:07:42 +0300 |
| References | <5452ADFD.5030801@it.uu.se> |
| Mime-Version | 1.0 |
| Content-Type | text/plain |
| X-Gmane-NNTP-Posting-Host | 89.169.229.68 |
| User-Agent | Gnus/5.13 (Gnus v5.13) Emacs/24.3 (gnu/linux) |
| Cancel-Lock | sha1:OsXLDfCUDOreHG+aPjVy/b75tPQ= |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.15 |
| Precedence | list |
| List-Id | General discussion list for the Python programming language <python-list.python.org> |
| List-Unsubscribe | <https://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 | <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.15350.1414757332.18130.python-list@python.org> (permalink) |
| Lines | 81 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1414757332 news.xs4all.nl 2843 [2001:888:2000:d::a6]:45657 |
| X-Complaints-To | abuse@xs4all.nl |
| Path | csiph.com!usenet.pasdenom.info!bete-des-vosges.org!feed.ac-versailles.fr!nerim.net!novso.com!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail |
| Xref | csiph.com comp.lang.python:80007 |
Show key headers only | View raw
Virgil Stokes <vs@it.uu.se> writes:
> While running a python program I need to save some of the data that is
> being created. I would like to save the data to a file on a disk
> according to a periodical schedule (e.g. every 10
> minutes). Initially, the amount of data is small (< 1 MB) but after
> sometime the amount of data can be >10MB. If a problem occurs during
> data creation, then the user should be able to start over from the
> last successfully saved data.
>
> For my particular application, no other file is being saved and the
> data should always replace (not be appended to) the previous data
> saved. It is important that the data be saved without any obvious
> distraction to the user who is busy creating more data. That is, I
> would like to save the data "in the background".
>
> What is a good method to perform this task using Python 2.7.8 on a
> Win32 platform?
There are several requirements:
- save data asynchroniously -- "without any obvious distraction to the
user"
- save data durably -- avoid corrupting previously saved data or
writing only partial new data e.g., in case of a power failure
- do it periodically -- handle drift/overlap gracefully in a documented
way
A simple way to do asynchronios I/O on Python 2.7.8 on a Win32 platform
is to use threads:
t = threading.Thread(target=backup_periodically, kwargs=dict(period=600))
t.daemon = True # stop if the program exits
t.start()
where backup_periodically() backups data every period seconds:
import time
def backup_periodically(period, timer=time.time, sleep=time.sleep):
start = timer()
while True:
try:
backup()
except Exception: # log exceptions and continue
logging.exception()
# lock with the timer
sleep(period - (timer() - start) % period)
To avoid drift over time of backup times, the sleep is locked with the
timer using the modulo operation. If backup() takes longer than *period*
seconds (unlikely for 10MB per 10 minutes) then the step may be
skipped.
backup() makes sure that the data is saved and can be restore at any
time.
def backup():
with atomic_open('backup', 'w') as file:
file.write(get_data())
where atomic_open() [1] tries to overcome multiple issues with saving
data reliably:
- write to a temporary file so that the old data is always available
- rename the file when all new data is written, handle cases such as:
* "antivirus opens old file thus preventing me from replacing it"
either the operation succeeds and 'backup' contains new data or it fails
and 'backup' contains untouched ready-to-restore old data -- nothing in
between.
[1]: https://github.com/mitsuhiko/python-atomicfile/blob/master/atomicfile.py
I don't know how ready atomicfile.py but you should be aware of the
issues it is trying to solve if you want a reliable backup solution.
--
Akira
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Re: Saving a file "in the background" -- How? Akira Li <4kir4.1i@gmail.com> - 2014-10-31 15:07 +0300
csiph-web