Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lernen Challenge: Producer-Consumer with Processes | Working with Processes
Quizzes & Challenges
Quizzes
Challenges
/
Python Multithreading and Multiprocessing

bookChallenge: Producer-Consumer with Processes

In many real-world applications, you need to coordinate work between different parts of a system running in parallel.

A common pattern for this is the producer-consumer scenario:

  • A producer process generates data (such as numbers) and puts them into a shared Queue;
  • At the same time, a consumer process retrieves data from the queue and processes it, for example by squaring each number and printing the result.

This pattern helps decouple data creation from data processing, making your programs more efficient and responsive when working with multiple processes.

Aufgabe

Swipe to start coding

Implement a producer-consumer pattern using two separate processes and a multiprocessing.Queue.

  • Complete the producer function so that it puts all numbers from the numbers list into the queue.
  • After all numbers, the producer should put a single 'DONE' marker into the queue to signal the consumer to stop.
  • Complete the consumer function so that it retrieves numbers from the queue. For each number, print the string 'Processed: {result}' where result is the square of the number.
  • The consumer should stop processing when it receives the 'DONE' marker and should not print anything for the 'DONE' marker.

Lösung

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 3. Kapitel 4
single

single

Fragen Sie AI

expand

Fragen Sie AI

ChatGPT

Fragen Sie alles oder probieren Sie eine der vorgeschlagenen Fragen, um unser Gespräch zu beginnen

close

bookChallenge: Producer-Consumer with Processes

Swipe um das Menü anzuzeigen

In many real-world applications, you need to coordinate work between different parts of a system running in parallel.

A common pattern for this is the producer-consumer scenario:

  • A producer process generates data (such as numbers) and puts them into a shared Queue;
  • At the same time, a consumer process retrieves data from the queue and processes it, for example by squaring each number and printing the result.

This pattern helps decouple data creation from data processing, making your programs more efficient and responsive when working with multiple processes.

Aufgabe

Swipe to start coding

Implement a producer-consumer pattern using two separate processes and a multiprocessing.Queue.

  • Complete the producer function so that it puts all numbers from the numbers list into the queue.
  • After all numbers, the producer should put a single 'DONE' marker into the queue to signal the consumer to stop.
  • Complete the consumer function so that it retrieves numbers from the queue. For each number, print the string 'Processed: {result}' where result is the square of the number.
  • The consumer should stop processing when it receives the 'DONE' marker and should not print anything for the 'DONE' marker.

Lösung

Switch to desktopWechseln Sie zum Desktop, um in der realen Welt zu übenFahren Sie dort fort, wo Sie sind, indem Sie eine der folgenden Optionen verwenden
War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 3. Kapitel 4
single

single

some-alt