title: Problems with multithreading queues implementations
body: <p>In the book <strong>Learning concurrency in Python</strong>, chapter 5 &quot;Communication between Threads&quot; there are several multithreading queue implementations and all have problems:</p>
  <ol>
  <li><a href="https://github.com/PacktPublishing/Learning-Concurrency-in-Python/blob/master/Chapter%2005/queueOperations.py" rel="nofollow noreferrer">https://github.com/PacktPublishing/Learning-Concurrency-in-Python/blob/master/Chapter%2005/queueOperations.py</a></li>
  </ol>
  <p>This implementation has condition that will never occur: <code>if item is None: break</code>, this is why it hangs without exiting. Either poison pills or other solution is needed here.</p>
  <ol start="2">
  <li><a href="https://github.com/PacktPublishing/Learning-Concurrency-in-Python/blob/master/Chapter%2005/lifoQueues.py" rel="nofollow noreferrer">https://github.com/PacktPublishing/Learning-Concurrency-in-Python/blob/master/Chapter%2005/lifoQueues.py</a></li>
  </ol>
  <p>This queue implementation has a bug: there is a race condition between <code>empty()</code> and <code>get()</code> calls. As a result, under some conditions the element can be stolen by a different thread from the queue between this calls, which will lead to blocking on <code>get()</code> call. To make this problem more obvious a sleep interval can be added between <code>empty()</code> and <code>get()</code> calls: <code>time.sleep(1)</code></p>
  <p>This implementation also has the same problem as the previous one: this condition will never occur: <code>if item is None: break</code>.</p>
  <p>I am not even talking about unused imports, although seeing that in a printed book suggests that the code was not reviewed.</p>
  <ol start="3">
  <li><a href="https://github.com/PacktPublishing/Learning-Concurrency-in-Python/blob/master/Chapter%2005/priorityQueue.py" rel="nofollow noreferrer">https://github.com/PacktPublishing/Learning-Concurrency-in-Python/blob/master/Chapter%2005/priorityQueue.py</a></li>
  </ol>
  <p>Same two problems here, nothing changed.</p>
  <ol start="4">
  <li><a href="https://github.com/PacktPublishing/Learning-Concurrency-in-Python/blob/master/Chapter%2005/fullQueue.py" rel="nofollow noreferrer">https://github.com/PacktPublishing/Learning-Concurrency-in-Python/blob/master/Chapter%2005/fullQueue.py</a></li>
  </ol>
  <p>This one will even not start (really?). <code>mySubscriber</code> has to be replaced with <code>myPublisher</code> just to let it run at least.</p>
  <p>Still it will never become full, because <code>maxsize</code> is not specified when <code>Queue</code> is created.</p>
  <p>Even if <code>maxsize</code> was specified, this queue has the same bug as previous ones: race condition between <code>empty()</code> and <code>get()</code> calls.</p>
  <ol start="5">
  <li><a href="https://github.com/PacktPublishing/Learning-Concurrency-in-Python/blob/master/Chapter%2005/queueJoin.py" rel="nofollow noreferrer">https://github.com/PacktPublishing/Learning-Concurrency-in-Python/blob/master/Chapter%2005/queueJoin.py</a></li>
  </ol>
  <p>This queue has a bug with condition that never occurs and no other bugs (believe it or not). The only thing that saved the author from the bug is that there is only one thread accessing the queue concurrently in this example, which results in no race conditions.</p>
  <p>What is actually happening with the books today? And especially: is there at least one good book on python concurrency? I have tried three books so far with no success:</p>
  <ul>
  <li><a href="https://www.packtpub.com/product/mastering-concurrency-in-python/9781789343052" rel="nofollow noreferrer">Mastering concurrency in Python</a></li>
  <li><a href="https://www.packtpub.com/product/learning-concurrency-in-python/9781787285378" rel="nofollow noreferrer">Learning concurrency in Python</a></li>
  <li><a href="https://rads.stackoverflow.com/amzn/click/com/1785289586" rel="nofollow noreferrer" rel="nofollow noreferrer">Python parallel programming cookbook</a></li>
  </ul>