| Store | Cart

Persistent Queue implementations?

From: Karl A. Krueger <kkru...@example.edu>
Thu, 26 Dec 2002 06:59:12 +0000 (UTC)
Karl A. Krueger <kkrueger at example.edu> wrote:
> I'm working on a cPickle-based queue now ... will post it when it's done> (as well as a corrected version of the shelf-based queue I posted, with> more of the Queue protocol working).

I shan't bother with the shelf, since Kevin Altis's advice was the right
way to do it.  Here's my cPickle-based queue, which appears to be both
the right thing and fast as heck.  I don't diddle around with making my
own semaphores -- inheriting from Queue.Queue does the job:


# PickleQueue -- persistent queue using cPickle

import Queue, cPickle

class PickleQueue(Queue.Queue):
    """A multi-producer, multi-consumer, persistent queue."""

    def __init__(self, filename, maxsize=0):
        """Initialize a persistent queue with a filename and maximum size.

        The filename is used as a persistent data store for the queue.
        If maxsize <= 0, the queue size is infinite.
        """
        self.filename = filename
        Queue.Queue.__init__(self, maxsize)
        if self.queue:
            self.esema.release()
        if self._full():
            self.fsema.acquire()

    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 = []
            else:
                # Some other I/O problem, reraise error
                raise err
        except EOFError:
            # File was null?  Continue ...
            self.queue = []

        # 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)

    def __sync(self):
        # Writes the queue to the pickle file.
        self.writefile.seek(0)
        cPickle.dump(self.queue, self.writefile, 1)
        self.writefile.flush()

    def _put(self, item):
        # Implements Queue protocol _put for persistent queue.
        self.queue.append(item)
        self.__sync()

    def _get(self):
        # Implements Queue protocol _get for persistent queue.
        item = self.queue[0]
        del self.queue[0]
        self.__sync()
        return item


-- 
Karl A. Krueger <kkrueger at example.edu>
Woods Hole Oceanographic Institution
Email address is spamtrapped.  s/example/whoi/
"Outlook not so good." -- Magic 8-Ball Software Reviews

Recent Messages in this Thread
Karl A. Krueger Dec 23, 2002 07:29 pm
Kevin Altis Dec 23, 2002 09:55 pm
Karl A. Krueger Dec 23, 2002 10:35 pm
Kevin Altis Dec 23, 2002 11:04 pm
dsavitsk Dec 23, 2002 11:54 pm
Aahz Dec 24, 2002 09:24 pm
Armin Steinhoff Dec 25, 2002 09:57 am
Karl A. Krueger Dec 25, 2002 06:12 pm
Karl A. Krueger Dec 26, 2002 06:59 am
Robin Becker Dec 26, 2002 10:36 am
darrell Dec 24, 2002 12:27 am
Aahz Dec 27, 2002 12:50 am
Karl A. Krueger Dec 27, 2002 07:29 pm
Aahz Dec 26, 2002 07:04 pm
Karl A. Krueger Dec 26, 2002 08:07 pm
Aahz Dec 26, 2002 03:56 am
Karl A. Krueger Dec 26, 2002 04:04 pm
Aahz Dec 25, 2002 03:56 pm
Messages in this thread