title: How to slice a queue?
body: <p>I have a queue which I process in a loop</p>
  
  <pre><code>while True:
      # a processing loop
      batch = []
      while True:
          e = q.get()
          if e:
              batch.append(e)
          else:
              # the queue is empty
              break
      do_something_with(batch)
      # wait a moment before emptying the queue again
      time.sleep(2)
  </code></pre>
  
  <p>The idea is to empty the queue, process its content and wait a moment before checking the content again.</p>
  
  <p>I sometimes hit a race condition where the queue is alimented while I <code>get()</code> an element and I end up with a growing <code>batch</code> which is never processed further.</p>
  
  <p>One solution would be to check <code>batch</code> size and process it when the size is right. This does not work if I have not that many events getting in the queue and <code>batch</code> never reaching the correct size - but I need the events (whatever is their number) to be processed and not wait until enough accumulate.</p>
  
  <p>The second solution is to build a check based on the size and on the time <code>batch</code> is idle -- this overly complicates the code.</p>
  
  <p>One good solution would be to <em>"get up to <code>n</code> elements from the queue at once"</em>. I could not find anything like that in the documentation. <strong>Is there a way to pop several elements at once from the queue (à la slicing for a list)?</strong></p>