from multiprocessing import pool
import Queue
class CheckableQueue(Queue.Queue):
"""A Checkable Queue that makes room for new values automatically.
Based on the standard FIFO Queue, this class is good for keeping
track of the most recent N items added to the queue. The oldest
items will automatically get pushed off when the Queue is Full and
a new item has been put()
"""
def __contains__(self, item):
with self.mutex:
boo = item in self.queue
print "%s in CheckableQueue: [%s]" % (item, boo)
return boo
def put(self, item, block=False, timeout=1):
print "Putting [%s]" % item
try:
Queue.Queue.put(self, item, block=block, timeout=timeout)
except Queue.Full:
try:
print "Queue Full! Removing one item."
self.get_nowait()
except Queue.Empty:
print "Queue Empty?"
pass
self.put(item)
QQ = CheckableQueue(maxsize=30)
def putter(thing):
return QQ.put(thing)
def isin(another):
return another in QQ
def main():
tpool = pool.ThreadPool()
ppool = pool.Pool()
tpool.map(putter, xrange(300))
tpool.map(isin, xrange(300))
ppool.map(putter, xrange(600, 900))
if __name__ == '__main__':
main()
Last active
August 29, 2015 14:04
-
-
Save stavxyz/1941a717a0d4a44c807e to your computer and use it in GitHub Desktop.
A checkable queue
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment