Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprende Working with Processes | Multithreading vs. Multiprocessing
Python Structural Programming

Working with Processes

Desliza para mostrar el menú

Python's multiprocessing module enables you to achieve true parallelism by spawning separate processes, each with its own Python interpreter and memory space. Unlike threads, which share memory and run in the same process, processes are completely isolated from each other. This isolation avoids the Global Interpreter Lock (GIL) limitation, allowing multiple CPU cores to work in parallel on CPU-bound tasks. After watching the video above, you should understand that process-based parallelism is especially useful when you need to perform heavy computations or take advantage of multiple cores for tasks that would otherwise be bottlenecked by the GIL. The multiprocessing module provides a familiar API similar to the threading module, making it easy to switch between threading and processing depending on your needs.

Use these classes and functions to build robust parallel applications, manage data sharing, and coordinate process execution in Python.

123456789101112131415
import multiprocessing import time def print_message(message, delay): time.sleep(delay) print(message) process1 = multiprocessing.Process(target=print_message, args=("Process 1 finished", 2)) process2 = multiprocessing.Process(target=print_message, args=("Process 2 finished", 1)) process1.start() process2.start() process1.join() process2.join()

This code sample demonstrates how to use Python's multiprocessing module to run tasks in parallel by creating separate processes. The example uses simple time delays and prints messages to show how processes can execute independently and simultaneously.

Key Concepts Illustrated

  • Process Creation:

    • You create new processes using the multiprocessing.Process class;
    • Each process is given a target function (print_message) and arguments to pass to that function.
  • Process Starting:

    • The start() method launches each process, allowing them to run concurrently;
    • Once started, each process runs its target function independently of the others.
  • Parallel Execution:

    • Both processes sleep for different durations (2 and 1 seconds) using time.sleep();
    • Because they run in parallel, the process with the shorter delay finishes first, even though it was started after the other.
  • Process Joining:

    • The join() method ensures that the main program waits for both processes to finish before moving on;
    • Without join(), the main program could exit before the processes complete.

This example shows that with multiprocessing, tasks can run at the same time on multiple CPU cores, making it ideal for CPU-bound operations that benefit from parallel execution.

question mark

Which statement best describes the difference between threads and processes in Python?

Selecciona la respuesta correcta

¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 3. Capítulo 4

Pregunte a AI

expand

Pregunte a AI

ChatGPT

Pregunte lo que quiera o pruebe una de las preguntas sugeridas para comenzar nuestra charla

Sección 3. Capítulo 4
some-alt