Why are my queues empty even though there are items in it?

Kayode Odeyemi dreyemi at gmail.com
Tue Sep 20 14:13:32 EDT 2011


Hello friends,

I'm writing some Python app that makes use of the multiprocessing and Queue
api to perform
transaction jobs.

My problem is that, the queue always report empty even though I can confirm
that there are items in it.

Below is the code I'm working with:

import multiprocessing
from multiprocessing import Process, Queue
from Queue import Empty

def process(**kwargs):
    q = kwargs['q']
    # Don't start the process if there's no item in queue
    if q.empty():
        print 'empty queue'
        multiprocessing.Process() # Process defaults to None
    else:
        p = multiprocessing.Process(target=transaction_queue, args=(kwargs))
        p.start()

def put_in_queue(items={}):
    print items

    q = multiprocessing.Queue()

    try:
        for k,v in items.iteritems():
            data = v #data is a dict
            timeout = data.get(u'timeout')

            # Put the transaction item in the queue at a specific timeout
            # period
            q.put(data, False, timeout)
            print "{0} put to queue".format(data)
            transaction_queue(q, data, timeout, False)

    except (KeyError, AttributeError, ValueError):
        print 'Incorrect data format'

def transaction_queue(queue, item, timeout, block=False):
    queue = multiprocessing.Queue()
    if item is not {} and timeout is not 0:
        print "Items are {0}".format(item)
        for i in range(len(item)):
            try:
                d = queue.get(block)
                print d
            except Empty:
                print 'Fees queue empty at %s' % (i)
            else:
                return process(q=queue, i=d, t=timeout)
    else:
        print 'No item in the queue to get'

A JSON POST from CURL calls put_in_queue callback:

 curl -v -H "Content-Type: application/json" -X POST --data
'fees={"fees":{"status":"pending","timeout":5},
"hostel":{"status":"pending","timeout": 3}}'
http://127.0.0.1:8000/transaction/add/ > post_data.txt

At code run, the output is:

{u'status': u'pending', u'timeout': 3} put to queue
Items are {u'status': u'pending', u'timeout': 3}
Fees queue empty at 0
Fees queue empty at 1
{u'status': u'pending', u'timeout': 5} put to queue
Items are {u'status': u'pending', u'timeout': 5}
Fees queue empty at 0
Fees queue empty at 1

Q: Why are my queues empty even though there are items in it?

Thanks you for the suggestions and answers.

-- 
Odeyemi 'Kayode O.
http://www.sinati.com. t: @charyorde
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20110920/90fa7009/attachment.html>


More information about the Python-list mailing list