This Python script demonstrates basic usage of threading by running multiple threads concurrently.
Each thread performs a simple task: printing a sequence of numbers with a delay between each print.
This example is designed to illustrate how threading can be used to perform concurrent operations in Python.
-
Definition: The
threading
module provides a way to run multiple threads (smaller units of a process) within a single process. Threads share the same memory space, which makes it easier to share data but also requires careful synchronization to avoid conflicts. -
Use Case: Ideal for I/O-bound tasks where tasks spend time waiting for external resources (e.g., file I/O, network operations). Threads can help in performing multiple tasks concurrently in a single process.
-
Key Features:
- Thread Class: Create and manage threads.
- Locks, Semaphores: Synchronize access to shared resources.
-
Example: Printing numbers with a delay using multiple threads.
-
Definition: The
multiprocessing
module allows the creation of multiple processes, each with its own Python interpreter and memory space. This helps bypass the Global Interpreter Lock (GIL) and is suitable for CPU-bound tasks. -
Use Case: Ideal for tasks that require heavy computation and can benefit from parallel execution across multiple CPU cores. Each process operates independently with its own memory space, which avoids conflicts but requires inter-process communication (IPC).
-
Key Features:
- Process Class: Create and manage processes.
- Pipes, Queues: Facilitate communication between processes.
-
Example: Performing computational tasks like processing large datasets in parallel.
-
Definition: The
asyncio
module provides a framework for writing asynchronous code using coroutines, event loops, and tasks. It allows for non-blocking operations and efficient management of multiple tasks within a single thread. -
Use Case: Best for I/O-bound and high-level structured network code where you need to handle many tasks concurrently without blocking. Suitable for tasks that involve waiting for I/O operations to complete.
-
Key Features:
- Event Loop: Manages and dispatches events.
- Coroutines: Define asynchronous functions that can be paused and resumed.
- Tasks: Represent coroutines scheduled for execution.
-
Example: Fetching web pages concurrently without blocking the execution.
Feature | threading | multiprocessing | asyncio |
---|---|---|---|
Definition | Provides a way to run multiple threads within a single process. Threads share the same memory space. | Allows the creation of multiple processes, each with its own memory space and Python interpreter. | Provides a framework for writing asynchronous code using coroutines, event loops, and tasks. |
Concurrency Model | Thread-based concurrency. | Process-based concurrency. | Asynchronous I/O with coroutines. |
Best Suited For | I/O-bound tasks where threads spend time waiting for external resources. | CPU-bound tasks that require parallel execution and can benefit from multiple CPU cores. | I/O-bound tasks where you need to handle many operations concurrently without blocking. |
Memory Space | Threads share the same memory space. | Each process has its own memory space. | All operations run within a single thread's memory space. |
Global Interpreter Lock (GIL) | Threads are limited by the GIL, which can restrict performance for CPU-bound tasks. | Processes do not share the GIL, allowing full parallelism for CPU-bound tasks. | Asyncio operates in a single thread, so GIL is not a limitation for I/O-bound tasks. |
Data Sharing | Easy to share data between threads, but requires synchronization mechanisms to avoid conflicts. | More complex data sharing using IPC mechanisms like Pipes and Queues. | No shared state between tasks; uses non-blocking I/O operations for concurrency. |
Overhead | Lower overhead compared to processes. | Higher overhead due to process creation and management. | Minimal overhead; only involves single-threaded execution. |
Task Scheduling | Managed by the operating system; threads are scheduled for execution concurrently. | Managed by the operating system; processes are scheduled for parallel execution. | Managed by the event loop; coroutines are scheduled and executed asynchronously. |
Example Use Case | Running multiple I/O operations concurrently, such as downloading files. | Performing heavy computations in parallel, such as processing large datasets. | Fetching multiple web pages concurrently without blocking. |