Previous Article link:-----------------------------------------------------------------------------------------------------------------------------------
get(), get_nowait() and Empty
Description:-
In the previous articles we came across several topics in which we used get() in our code.
There we haven't used the default arguments that get() has because they are frequently not used
and they are optional. get_nowait() is some how similar to get(). If we talk about Empty then it is one
of the exception in Queue module like Full.
SYNTAX:- queue.Empty #Exception
Exception raised when non-blocking get() or get_nowait() is called on a Queue object
which is empty.
So in this article we will discuss about these three topics.
It is used to get items from the queue, block and timeout are the optional arguments.
1) If block is True and timeout is None(the default), block if necessary until element
is available.
2) If block is True and timeout is a positive number, it blocks at most timeout seconds
and raises the Empty exception if no free slot was available within that time.
3) If block is False, return an item if one is immediately available else raise the
Empty exception( timeout is ignored in this case).
Let's discuss each of these cases with examples.
CASE - 1:
SYNTAX:- queue.get(block = True, timeout = None)
All the examples that we came across till now uses this syntax for implementation.
There we didn't put block and timeout but by default they are True and None. So we
need not to mention it. Here block is True which means it will block everything if
necessary until a new element is available.
:------> Example <------:
import queue
q = queue.Queue(maxsize=5)
for i in range(1, 6):
q.put(i)
while not q.empty():
print(q.get(block=True, timeout=None))
Output:-
"C:UsersShubhani_PandeyDesktopHarshit Pandeypythonpython.exe" "C:/Users/Shubhani_Pandey/Desktop/Harshit Pandey/python_files/Article-7.py"
1
2
3
4
5
Process finished with exit code 0
Now try to put another q.get(block=True, timeout=5) after outside while loop and run your code. You will notice that after retrieving 1 to 5 elements your output will stop and will not end until you
yourself stop it because here block=True and timeout=None which means block will continue it's function until a new element is available but the queue is empty so it will never unblock.
CASE - 2:
SYNTAX:- queue.get(block = True, timeout = integer value)
With this syntax we can assign an integer value to the timeout. By doing so the block
will work till this timeout value and if no element is found it will raise the Empty
exception.
:------> Example <------:
import queue
q = queue.Queue(maxsize=5)
for i in range(1, 6):
q.put(i, block=True, timeout=5)
try:
for i in range(1, 7):
print(q.get(block=True, timeout=5))
except queue.Empty:
print("Empty exception")
pass
Output:-
"C:UsersShubhani_PandeyDesktopHarshit Pandeypythonpython.exe" "C:/Users/Shubhani_Pandey/Desktop/Harshit Pandey/python_files/Article-7.py"
1
2
3
4
5
Empty exception
Process finished with exit code 0
If you carefully look at the above code then you have noticed that the maxsize of queue is 5 and elements inserted are also 5. But in try statement for loop is running 6 times with a get() function
in it having block=True and timeout=5. So when you run the code get() function will retrieve 5
elements and immediately print them. But after that there is nothing to retrieve so block will wait
till timeout and will unblock after it and raise Empty exception. Notice the output carefully while
running the code that 1 to 5 elements will retrieve immediately and Empty exception will be raised
after 5 seconds.
CASE - 3:
SYNTAX:- queue.get(block = False)
OR
queue.get_nowait(False)
The syntax above shows that get_nowait(item) is similar to get(item, False). We can
use either of it in the code. One thing to notice that timeout is not there in the argument.
Even if you mention it then it will be ignored because block is False. If element is not
available then immediately Full exception will be raised even if you provide timeout
with certain integer value. In the above try to change block=True to block=False,
you will notice the change.
Next article link:-
Comments