DEV Community

moogoo
moogoo

Posted on • Edited on

1 1

Python queue.Queue

Python has 3 ways to implement a "queue" (FIFO rule).

  • list
  • collections.deque
  • queue.Queue

list is build-in, multi purpose structure. If get item out from a list, need to shift all item, may slow. collections.deque: list-like container with fast appends and pops on either end. Here introduce more about Queue:

from queue import Queue

q = Queue(5) # 5 is maxsize of the queue [1]
q.put('foo') # put item into a queue
q.put('bar')

print(q.qsize()) # approximate [2] size of the queue
>>> 2
print(q.full()) # check full or not
>>> False
print(q.empty()) # check empty or not
>>> False
a = q.get()
print(a)
>>> foo
b = q.get()
print(b)
>>> bar
c = q.get() 
# blocking  ... queue is empty, wait until an item is available. 

get_nowait() # get(False), if not available, raise queue.Empty

# blocking process
task_done() & join()
Enter fullscreen mode Exit fullscreen mode

[1]
If queue is full, no free slow, put() will block while available

[2]
qsize() > 0 doesn’t guarantee that a subsequent get() will not block, nor will qsize() < maxsize guarantee that put() will not block.

Image of Docusign

🛠️ Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay