Search code examples
python-3.xqueuestackmean-stack

what is difference in get() and get_nowait() in stack?


I am not able to find the Difference between these two Functions of Stack. get():- returns elements. get_nowait() :- also returns element. then what makes them different??


Solution

  • The difference is that one blocks and the other does not. From the docs:

    Queue.get(block=True, timeout=None)

    Remove and return an item from the queue. If optional args block is true and timeout is None (the default), block if necessary until an item is available. If timeout is a positive number, it blocks at most timeout seconds and raises the Empty exception if no item was available within that time. Otherwise (block is false), return an item if one is immediately available, else raise the Empty exception (timeout is ignored in that case).

    Queue.get if the block parameter is True will block until either a element is pushed to the queue or the timeout if given is exceeded.

    Queue.get_nowait()

    Equivalent to get(False).

    Queue.get_no_wait() will never block, but will return Queue.Empty if the queue is empty.

    The same is true for the stack implementations.