Simple python pool
Published by packi on

Since the beginning of this year (that’s 2012, in the case I’m not writing any articles for a year, again) I’ve been working with Python. It took some time getting used to the indentation but since then I’m really enjoying it. One reason is that it allows you to do complex things extremely simple.
I had to write a pool for some objects that needed to be thread-safe. Thats what I came up with:

class Pool(object):
    def __init__(self, items):
        self.queue = Queue(len(items))
        for item in items:

    def item(self):
        item = self.queue.get()
            yield item


p = Pool([SomeObject()])

with item = p.item():

The item gets returned right after it has been used, without any knowledge about the pool.