Calling Queue experts

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Mon Mar 26 09:07:35 EDT 2007


En Mon, 26 Mar 2007 07:29:32 -0300, jrpfinch <jrpfinch at gmail.com> escribió:

> Got it.  New PickleQueue class should be as follows:

Only a comment:

>     def _init(self, maxsize):
>         # Implements Queue protocol _init for persistent queue.
>         # Sets up the pickle files.
>         self.maxsize = maxsize
>         try:
>             self.readfile = file(self.filename, 'r')
>             self.queue = cPickle.load(self.readfile)
>             self.readfile.close()
>         except IOError, err:
>             if err.errno == 2:
>                 # File doesn't exist, continue ...
>                 self.queue = Queue.deque()
>             else:
>                 # Some other I/O problem, reraise error
>                 raise err
>         except EOFError:
>             # File was null?  Continue ...
>             self.queue = Queue.deque()
>
>
>         # Rewrite file, so it's created if it doesn't exist,
>         # and raises an exception now if we aren't allowed
>         self.writefile = file(self.filename, 'w')
>         cPickle.dump(self.queue, self.writefile, 1)

self.readfile may be left open in case of error, I'd use a try/finally.  
And since it isn't used anywhere, I'd just use a local variable instead of  
an instance attribute.
And the final write is not necesary when you have just read it - and  
alters the "last-modified-time" (that may not be relevant for you, of  
course, but as a general tool it may confuse other users).

-- 
Gabriel Genellina




More information about the Python-list mailing list