Previous article link:- ----------------------------------------------------------------------------------------------------------------------------------
put(), put_nowait() and Full
Description:-
In the previous articles we came across several topics in which we used put(item) in our code.
There we haven't used the default arguments that put() has because they are frequently not used
and they are optional. Put_nowait() is some how similar to put(). If we talk about Full then it is one
of the exception in Queue module.
SYNTAX:- queue.Full #Exception
Exception raised when non-blocking put() or put_nowait() is called on a Queue object
which is full.
So in this article we will discuss about these three topics.
It is used to put items in the queue. block and timeout are the optional arguments.
1) If block is True and timeout is None(the default), block if necessary until a free slot
is available.
2) If block is True and timeout is a positive number, it blocks at most timeout seconds
and raises the Full exception if no free slot was available within that time.
3) If block is False, put an item on the queue if a free slot is immediately available else
raise the Full exception( timeout is ignored in this case).
Let's discuss each of these cases with examples.
CASE - 1:
SYNTAX:- queue.put( item, 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 slot for the element to be inserted 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())
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
While running the program do notice that the output appears instantly because timeout=None because timeout decides till what time block should work.
CASE - 2:
SYNTAX:- queue.put( item, 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 the empty slot is available then put the item
there otherwise raise the Full exception.
:------> Example <------:
import queue
q = queue.Queue(maxsize=5)
try:
for i in range(1, 7):
q.put(i, block=True, timeout=5)
except queue.Full:
print("Full exception ")
pass
finally:
while not q.empty():
print(q.get())
Output:-"C:UsersShubhani_PandeyDesktopHarshit Pandeypythonpython.exe" "C:/Users/Shubhani_Pandey/Desktop/Harshit Pandey/python_files/Article-7.py"
Full exception
1
2
3
4
5
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 but I'm inserting 6 elements. Therefore full exception will be raised. And also notice that timeout = 5. That
means block will be True for 5 seconds and look for the empty slot for the element. After that it will
unblock and raise Full exception. When you run the code notice that Full exception will be raised
after 5 seconds on your screen with the elements that are inserted in the queue.
CASE - 3:
SYNTAX:- queue.put( item, block = False)
OR
queue.put_nowait(item)
The syntax above shows that put_nowait(item) is similar to put(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 the slot is not
available then immediately Full exception will be raised even if you provide timeout
with certain integer value.
:------> Example <------:
import queue
q = queue.Queue(maxsize=5)
try:
for i in range(1, 7):
q.put(i, block=False, timeout=5)
except queue.Full:
print("Full exception ")
pass
finally:
while not q.empty():
print(q.get())
Output:-
"C:UsersShubhani_PandeyDesktopHarshit Pandeypythonpython.exe" "C:/Users/Shubhani_Pandey/Desktop/Harshit Pandey/python_files/Article-7.py"
Full exception
1
2
3
4
5
Process finished with exit code 0
For better understanding I provided timeout in the argument though it has no effect at all. Run the above code and you will notice that Full exception will be raised immediately without waiting for
5 seconds. In this example instead of put() you can also use put_nowait()
Next article link:-
Comments