From the course: Complete Guide to Parallel and Concurrent Programming in Python
Unlock the full course today
Join today to access over 24,900 courses taught by industry experts.
Producer-consumer processes: Python demo - Python Tutorial
From the course: Complete Guide to Parallel and Concurrent Programming in Python
Producer-consumer processes: Python demo
- [Instructor] We were able to resolve the buffer overflow problem in the previous producer/consumer example by adding an additional consumer thread. That worked because we simulated the time it took for a producer to add items to the queue and for the consumer to process those items by using the sleep statements on lines 15 and 25. When a thread is sleeping, it does not use CPU resources, so that scenario represents a situation where the consumers are performing an I/O bound task, like downloading files over a network. But what would happen if the consumer's task was CPU-intensive instead? Perhaps the producer was streaming data that the consumer needed to process in near real time. To simulate that scenario, I'll define a new helper function called cpu_work, and I'll pass it in argument representing the amount of CPU work to simulate. Within the function, I'll simply create a variable named x and initialize it to zero. And then I'll use a for loop to increment x by the number of…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.