Queues

Learn how to use the queue.Queue for inter-process communication and building a concurrent text content search engine in Python.

Overview

If we need more control over communication between processes, the queue.Queue data structure is useful. There are several variants offering ways to send messages from one process to one or more other processes. Any picklable object can be sent into a Queue, but remember that pickling can be a costly operation, so keep such objects small. To illustrate queues, let’s build a little search engine for text content that stores all relevant entries in memory.

Example

A particular search engine scans all files in the current directory in parallel. A process is constructed for each core on the CPU. Each of these is instructed to load some of the files into memory. Let’s look at the function that does the loading and searching:

Get hands-on with 1200+ tech skills courses.