Linux

Python Queue with Threads

Up to this you have understood the concept of Python thread and Python queue. In this section you will understand the Python thread with queue.
Let us discuss our next example of Python thread with queue.
Python threads with queue
Python Thread with Queue
Click to view code
In above program 2 Python threads are created, started and called the function worker(). Next for loop put items into the Python queue. You can see different results of two outputs. In first output both threads access function worker() at same time. In next output first thread completes its work then second thread does it. So what happen if you put more items in the Python queue.
Let check in our next example.
Python thread with Python Queue example
Python Thread with Python Queue
Click to view code
It just print 0 and 1 , not printing 2 and 3 means two Python threads are taking only two elements from the Python Queue. We have to use will while loop.
Let us discuss next example.
Python Thread with Python Queue with while loop
Python Thread with Python Queue with while loop
Click to view code
In above program we have done one amendments first fill the Python queue first then thread get the items from the Python queue.
Now we will discuss complicated example.
Python  thread with Python Queue
Python Thread with Queue
Click to view code
I have use infinite while loop and Python thread are non-daemon thread, So main thread wait for non-daemon threads to exit.
In next example you will see the Python daemon thread.
Python Daemon thread with Queue with join()
Daemon Thread with Queue
Click to view code
In above code new things is q.task_done(). After the work is done, send a signal to the Python queue with q.task_done() that the task has been completed. Join on (q.join()) the Python queue, which really means to wait until the Python queue is empty, and then exit the main program. The join() function blocks until all items in the Python queue have been gotten and processed. Whenever an item is added to the Python queue the count of unfinished tasks goes up. Whenever a consumer thread calls q.task_done() the count goes down. It is indicate that the item was retrieved and all work on it is complete. When the count of unfinished tasks drops to zero, join() unblocks.

Up to this level you see the Python thread with queue. But output is not printing in the order, when you create Python threads, all are simultaneously trying to access the queue. If you want uniformity in out-put you have to use lock mechanism.
Let us discuss our next, however complicated example with threading class.
Python daemon thread with queue with lock
Python Daemon Thread with Queue
Click to view code
So much colourful, I have mentioned that all Python threads are created simultaneously that's why green box in out-put screen is not uniform. Only one Python thread can acquire lock at one time. you can see %%%% in out-put screen, it means Python thread entered in while loop. In out-put screen you see the Python thread number and queue item.




admin