Re: Question about pickle

2013-06-26 Thread Phu Sam
f.seek(0)  really does the trick.

Danke sehr,

Phu


On Tue, Jun 25, 2013 at 6:47 AM, Peter Otten <__pete...@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
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Question about pickle

2013-06-25 Thread Peter Otten
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


Question about pickle

2013-06-25 Thread Phu Sam
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)

  pickle.dump(tb, f)

  fcntl.flock(f.fileno(),fcntl.LOCK_UN)
  f.close()


What could be the problem here?
What mode should I use to open the file to allow both pickle.load and
pickle.dump?

Thanks,


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