Describing the queue Module
- The queuemodule implements various mulitproducer multiconsumer queues
- These can be used to safely exchange information between multiple threads
- 
The queue module defines three different queue classes: - Queue
- LifoQueue
- PriorityQueue
 
- An instance qof any of these constructors can be created
Describing Queue(maxsize=0)
- This class creates a FIFO queue
- maxsizeis the maximum number of items that can be placed in the queue
- If maxsizeis omitted or , then the queue size is infinite
Describing LifoQueue(maxsize=0)
- This class creates a LIFO queue
- A LIFO queue is also known as a stack
Describing PriorityQueue(maxsize=0)
- This class creates a priority queue in which items are ordered from lowest to highest priority
- When working with this queue, items should be tuples
- These tuples should take the form of (priority, data)
Summarizing Instance Methods
- 
Each qinstance should callgeton the queue to retrieve a task- This call will block if no tasks are available
- This will causes workers to go idle until one becomes available
 
- Then, the worker should execute the task
- Once the tasks is done, task_doneshoul dbe called onq
- We can put tasks in qby callingput
- Calling joinonqwill wait until all pending tasks have completed
Benefit of Queues
- 
Using queues instead of thread pools has the following benefit: - Not creating threads, which is expensive
- Not destroying threads, which is expensive
 
- The worker threads will run continuously
- When no tasks are in the queue, the queue will sleep
- This won't use any CPU time
References
Previous
Next