title: Clean way to get near-LIFO behavior from multiprocessing.Queue? (or even just *not* near-FIFO)
body: <p>Does anyone know a clean way to get near-LIFO or even not near-FIFO (e.g. random) behavior from <code>multiprocessing.Queue</code>?</p>
  
  <p><strong>Alternative Question: Could someone point me to the code for the thread that manages the actual storage structure behind <code>multiprocessing.Queue</code>?</strong> It seems like it would be trivial within that to provide approximately LIFO access, but I got lost in the rabbit hole trying to find it.</p>
  
  <p>Notes:</p>
  
  <ol>
  <li>I believe <code>multiprocessing.Queue</code> <a href="http://bugs.python.org/issue4999" rel="nofollow">does not guarantee order</a>. Fine. But it is near-FIFO so near-LIFO would be great.</li>
  <li>I could pull all the current items off the queue and reverse the order before working with them, but I prefer to avoid a kludge if possible.</li>
  </ol>
  
  <hr>
  
  <p>(edit) To clarify: I am doing a CPU bound simulation with <code>multiprocessing</code> and so can't use the specialized queues from <code>Queue</code>. Since I haven't seen any answers for a few days, I've added the alternative question above.</p>
  
  <hr>
  
  <p>In case it is an issue, below is slight evidence that <code>multiprocessing.Queue</code> is near-FIFO. It just shows that in a simple case (a single thread), it is perfectly FIFO on my system:</p>
  
  <pre><code>import multiprocessing as mp
  import Queue
  
  q = mp.Queue()
  
  for i in xrange(1000):
      q.put(i)
  
  deltas = []
  while True:
      try:
          value1 = q.get(timeout=0.1)
          value2 = q.get(timeout=0.1)
          deltas.append(value2-value1)
      except Queue.Empty:
          break
  
  #positive deltas would indicate the numbers are coming out in increasing order
  min_delta, max_delta = min(deltas), max(deltas)
  avg_delta = sum(deltas)/len(deltas)
  
  print "min", min_delta
  print "max", max_delta
  print "avg", avg_delta
  </code></pre>
  
  <p>prints: min, max, and average are exactly 1 (perfect FIFO)</p>