Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.mixmin.net!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed2.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.016 X-Spam-Evidence: '*H*': 0.97; '*S*': 0.00; 'modified': 0.07; 'modify': 0.07; 'subject:Question': 0.07; 'assuming': 0.09; 'f.close()': 0.09; 'here?': 0.09; 'works.': 0.09; 'cc:addr:python-list': 0.11; 'def': 0.12; 'dict': 0.16; 'dictionary.': 0.16; 'f.seek(0)': 0.16; 'record,': 0.16; 'status)': 0.16; 'to:addr:web.de': 0.16; 'fix': 0.17; 'wrote:': 0.18; 'file,': 0.19; 'first.': 0.19; 'skip:f 30': 0.19; 'cc:addr:python.org': 0.22; 'exists': 0.24; 'versions': 0.24; 'file.': 0.24; 'cc:2**0': 0.24; 'cc:no real name:2**0': 0.24; '>': 0.26; 'gets': 0.27; 'header:In-Reply-To:1': 0.27; 'am,': 0.29; 'mode': 0.30; 'message-id:@mail.gmail.com': 0.30; 'url:mailman': 0.30; 'usually': 0.31; '25,': 0.31; 'time:': 0.31; 'file': 0.32; 'open': 0.33; 'url:python': 0.33; 'guess': 0.33; 'position.': 0.33; 'could': 0.34; 'problem': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'really': 0.36; 'url:listinfo': 0.36; 'method': 0.36; 'url:org': 0.36; 'should': 0.36; 'two': 0.37; 'starting': 0.37; 'skip:& 10': 0.38; 'writes': 0.38; 'does': 0.39; 'url:mail': 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; 'danke': 0.84; 'dict,': 0.84; 'moves': 0.84; 'otten': 0.84; 'trick.': 0.84; 'writing,': 0.84; 'opens': 0.91; '2013': 0.98 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; bh=NtEKB+OGgZtTZQDC8Ofk4/6kcXADGSQyLmYCO3WdZMM=; b=j7q7s/tZjLsmDC0ns9I29KDrBEFNpdMhWB0t4Zt7S4fsRdakhZrgKQjqlnz1nq5RAN W5wiIua4S8dDCIHQE2m6mvARyaWdQlGsY73jF6HZ4CBi2sT/9Nu9VUS97y5vo3CPkSU9 zqbgF92NSEq1PuXSrLF2LiNXQlrGMlSK2VoLn6BvJ7LVFPZDFdRHy9wbPhphDqcQvuPe wc7I9HmviAxdh5W69HUoTlRtzpMiAShuGjRrFAn5Sw6FvkzXNn4NdNIgJd1Iyz+UKUyY 1ZID3UgMLYaUXOFplI+K4e4KDO8MSqBGbSv1sYPMPx4ivyKK7PutSCXVE9MGlzvZx4uX bvjQ== MIME-Version: 1.0 X-Received: by 10.66.251.202 with SMTP id zm10mr2262974pac.53.1372206144060; Tue, 25 Jun 2013 17:22:24 -0700 (PDT) In-Reply-To: References: Date: Tue, 25 Jun 2013 17:22:23 -0700 Subject: Re: Question about pickle From: Phu Sam To: Peter Otten <__peter__@web.de> Content-Type: multipart/alternative; boundary=047d7b15a385c686a104e003a3b0 X-Mailman-Approved-At: Wed, 26 Jun 2013 12:41:29 +0200 Cc: python-list@python.org X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 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: 131 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1372243291 news.xs4all.nl 16002 [2001:888:2000:d::a6]:38307 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:49244 --047d7b15a385c686a104e003a3b0 Content-Type: text/plain; charset=ISO-8859-1 f.seek(0) really does the trick. Danke sehr, Phu On Tue, Jun 25, 2013 at 6:47 AM, Peter Otten <__peter__@web.de> wrote: > 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. > > -- > http://mail.python.org/mailman/listinfo/python-list > --047d7b15a385c686a104e003a3b0 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable
f.seek(0) =A0really does the trick.

Danke sehr,

Phu


On Tue, Jun 25, 2013 = at 6:47 AM, Peter Otten <__peter__@web.de> wrote:
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):
> =A0 =A0 =A0 =A0 =A0 f =3D open(fp, 'rw+')
> =A0 =A0 =A0 =A0 =A0 fcntl.flock(f.fileno(),fcntl.LOCK_EX | fcntl.LOCK_= NB)
>
> =A0 =A0 =A0 =A0 =A0 tb =3D pickle.load(f)
>
> =A0 =A0 =A0 =A0 =A0 self.modifyDict(tb, stn, status)

=A0 =A0 =A0 =A0 =A0 =A0 f.seek(0)

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

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

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<= br> > 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.

--
http://mail.python.org/mailman/listinfo/python-list

--047d7b15a385c686a104e003a3b0--