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


Groups > comp.lang.python > #49159

Re: Question about pickle

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.001
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'modified': 0.07; 'modify': 0.07; 'subject:Question': 0.07; 'assuming': 0.09; 'f.close()': 0.09; 'here?': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'works.': 0.09; 'def': 0.12; 'dict': 0.16; 'dictionary.': 0.16; 'f.seek(0)': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'record,': 0.16; 'status)': 0.16; 'fix': 0.17; 'wrote:': 0.18; 'file,': 0.19; 'first.': 0.19; 'skip:f 30': 0.19; 'header:User-Agent:1': 0.23; 'exists': 0.24; 'versions': 0.24; 'file.': 0.24; 'gets': 0.27; 'header:X -Complaints-To:1': 0.27; 'mode': 0.30; 'usually': 0.31; 'time:': 0.31; 'file': 0.32; 'open': 0.33; 'guess': 0.33; 'position.': 0.33; 'could': 0.34; 'problem': 0.35; 'but': 0.35; 'method': 0.36; 'should': 0.36; 'two': 0.37; 'starting': 0.37; 'writes': 0.38; 'to:addr:python-list': 0.38; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'skip:u 10': 0.60; 'read': 0.60; 'first': 0.61; 'back': 0.62; "you'll": 0.62; 'sam': 0.68; '(first)': 0.84; 'dict,': 0.84; 'moves': 0.84; 'writing,': 0.84; 'opens': 0.91
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Peter Otten <__peter__@web.de>
Subject Re: Question about pickle
Date Tue, 25 Jun 2013 15:47:59 +0200
Organization None
References <CANjxQ6wWvT6zmn46oC=eQS0+dqtvkRpn2nKzdbmisLfm34LZPg@mail.gmail.com>
Mime-Version 1.0
Content-Type text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding 7Bit
X-Gmane-NNTP-Posting-Host p50848be5.dip0.t-ipconnect.de
User-Agent KNode/4.7.3
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 <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.3828.1372168083.3114.python-list@python.org> (permalink)
Lines 43
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1372168083 news.xs4all.nl 15934 [2001:888:2000:d::a6]:38230
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:49159

Show key headers only | View raw


Phu Sam wrote:

> I have a method that opens a file, lock it, pickle.load the file into a
> dictionary.
> I then modify the status of a record, then pickle.dump the dictionary back
> to the file.
> 
> The problem is that the pickle.dump never works. The file never gets
> updated.
> 
> def updateStatus(self, fp, stn, status):
>           f = open(fp, 'rw+')
>           fcntl.flock(f.fileno(),fcntl.LOCK_EX | fcntl.LOCK_NB)
> 
>           tb = pickle.load(f)
> 
>           self.modifyDict(tb, stn, status)

            f.seek(0)

>           pickle.dump(tb, f)
> 
>           fcntl.flock(f.fileno(),fcntl.LOCK_UN)
>           f.close()
> 
> 
> What could be the problem here?

pickle.load() moves the file position to the end of the (first) pickle.
pickle.dump() writes the modified dict starting at the current position. You 
end up with two versions of the dict, but you'll only ever read the first.

The fix is to go back to the start of the file with f.seek().

> What mode should I use to open the file to allow both pickle.load and
> pickle.dump?

Assuming that the file already exists when updateStatus() is invoked for the 
first time: "r+b".

I usually open the file twice, once for reading and then for writing, but I 
guess that would interfere with locking.

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


Thread

Re: Question about pickle Peter Otten <__peter__@web.de> - 2013-06-25 15:47 +0200

csiph-web