December 2023
Intermediate to advanced
504 pages
11h 43m
English
Queues are common in many applications, ranging from printing jobs to background workers in web applications.
Let’s say we’re programming a simple Python interface for a printer that can accept printing jobs from various computers across a network. We want to make sure we print each document in the order in which it was received.
This code uses our implementation of the Queue class from earlier:
| | import queue |
| | |
| | |
| | class PrintManager: |
| | |
| | def __init__(self): |
| | self.queue = queue.Queue() |
| | |
| | def queue_print_job(self, document): |
| | self.queue.enqueue(document) |
| | |
| | def run(self): |
| | while self.queue.read(): |
| | self.print_document(self.queue.dequeue()) |
| | |
| | def print_document(self, document): |
| | # Code to run ... |
Read now
Unlock full access