Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Challenge: The Parallel Square Collector | Multithreading vs. Multiprocessing
Python Structural Programming
Sektion 3. Kapitel 5
single

single

Challenge: The Parallel Square Collector

Stryg for at vise menuen

Multiprocessing in Python allows you to run multiple processes in parallel, each with its own Python interpreter and memory space. This approach is especially useful for CPU-bound tasks, where you want to leverage multiple CPU cores to perform computations faster. By using multiprocessing, you can significantly improve performance for operations that require heavy computation, such as data processing, scientific calculations, or simulations.

Note
Note

The multiprocessing.Manager class provides a way to create shared objects - such as lists and dictionaries - that can be safely accessed and modified by multiple processes. When you need to collect results or coordinate data between separate Process instances, always use a Manager to create these shared objects. Regular Python lists and dictionaries are not shared between processes, so changes made in one process will not be visible to others. With a Manager list, each process can append or modify data, and all changes will be visible to every process using that shared object.

123456789101112131415161718192021
from multiprocessing import Process, Manager def add_value(value, shared_list): shared_list.append(value) if __name__ == "__main__": manager = Manager() shared_list = manager.list() processes = [] values = [10, 20, 30, 40, 50] for v in values: p = Process(target=add_value, args=(v, shared_list)) processes.append(p) p.start() for p in processes: p.join() print("Collected results:", list(shared_list))

This code demonstrates how to use multiprocessing.Manager to create a shared list that multiple processes can access and modify. Each process computes the square of a number and appends its result to the shared list. By using a Manager list, you ensure that all processes can safely share and collect data, making it possible to gather results from separate processes running in parallel.

Opgave

Swipe to start coding

Write a function called compute_square that takes a single integer and a results list as arguments, computes the square of the integer, and appends the result to the results list.

Then, using the multiprocessing.Process class, create a separate process for each number in the numbers list to compute its square in parallel. Collect the results from all processes and store them in the results variable.

  • Implement the compute_square function to take two arguments: the integer to square and the results list.
  • In compute_square, compute the square of the integer and append it to the results list.
  • For each number in numbers, create a Process targeting compute_square with the number and results as arguments.
  • Start all processes, then join them to ensure all computations complete before checking the results.
  • Store all computed squares in the results list.

Løsning

Switch to desktopSkift til skrivebord for at øve i den virkelige verdenFortsæt der, hvor du er, med en af nedenstående muligheder
Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 3. Kapitel 5
single

single

Spørg AI

expand

Spørg AI

ChatGPT

Spørg om hvad som helst eller prøv et af de foreslåede spørgsmål for at starte vores chat

some-alt