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

MRAB python at mrabarnett.plus.com
Tue Sep 20 20:42:38 CEST 2011


On 20/09/2011 19:13, Kayode Odeyemi wrote:
> 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:
>
[snip]
>
> def transaction_queue(queue, item, timeout, block=False):
>      queue = multiprocessing.Queue()

This line creates a new empty queue, hiding the one which was passed in.

>      if item is not {} and timeout is not 0:

`not {}` has the value True, so `item is not {}` means `item is True`.
The `is` checks for identity, not equality, so this is true only if `item`
actually has the value True or 1 (and this is an implementation-dependent
behaviour).

>          print "Items are {0}".format(item)
>          for i in range(len(item)):

You're trying to get as many items from the queue as there are items in
the dict `item`, which looks wrong to me.

>              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?
>
What makes you think there are items in it? You print out `item`, which
is not the queue.

> Thanks you for the suggestions and answers.
>



More information about the Python-list mailing list