From the course: Python Parallel and Concurrent Programming Part 2

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Producer-consumer threads: Python demo

Producer-consumer threads: Python demo - Python Tutorial

From the course: Python Parallel and Concurrent Programming Part 2

Start my 1-month free trial

Producer-consumer threads: Python demo

- [Instructor] This Python example program has two primary functions. A soup producer on line 10, and a soup consumer on line 17. I use those functions to create two threads down in the program's main section. As their names suggest, the soup producer thread is generating bowls of soup which I represent with strings for the consumer to eat. The producer and consumer use a queue to pass objects between them. When we create that queue named serving line on line eight we set the max size argument to five. Which means the queue is limited to hold up to five items at a time. We created the serving line queue using Python's queue module which is designed for exchanging objects between multiple threads. The queue class already implements all of the necessary locks and synchronization mechanisms for multiple producers and consumer threads to use it concurrently. So, conveniently we don't have to worry about that. Now, looking at the soup producer function, the for loop on line 11 is used to…

Contents