[Avg. reading time: 11 minutes]
GIL (Global Interpreter Lock)
The Global Interpreter Lock (GIL) is a mutex (mutual exclusion) that allows only one thread to hold control of the Python interpreter. This means only one thread can execute Python bytecode at a time.
By design, GIL affects the performance of Python programs by limiting true parallelism on multi-core processors.
To learn more here is an interesting article https://realpython.com/python-gil/
Thread: Independent sequence of instructions within a program.
Parallel & Concurrent Programming
Concurrent Programming
Managing multiple tasks that can start, run, and complete in overlapping time periods.
- Tasks don’t necessarily run at the same exact moment
Like a chef managing multiple dishes in a kitchen - switching between tasks.
Used for: Improved program structure and responsiveness.
Parallel Programming
Multiple tasks or calculations happening simultaneously
- Requires multiple processors/cores.
Like multiple chefs working in different kitchen stations simultaneously
Used for: Improved performance through simultaneous execution.
Types of Concurrency
Thread Based
Pros of Threading:
- Good for I/O-bound tasks (reading files, network operations)
- Shared memory space - easy to share data between threads
- Lightweight compared to processes
Cons of Threading:
- Global Interpreter Lock (GIL) limits true parallelism
- Complex synchronization needed (locks, semaphores)
- Race conditions and deadlocks are common pitfalls
| Keyword | Description |
|---|---|
threading.Thread() | Creates a new thread object |
thread.start() | Starts thread execution |
thread.join() | Waits for thread completion |
threading.Lock() | Creates mutex lock for thread synchronization |
threading.Event() | Event synchronization between threads |
threading.Semaphore() | Limits access to shared resources |
threading.get_ident() | Gets current thread identifier |
Process Based
Pros of Multiprocessing:
- True parallelism (bypasses GIL)
- Better for CPU-intensive tasks
- Process isolation provides better stability
Cons of Multiprocessing:
- Higher memory usage
- Slower process creation
- More complex data sharing between processes
| Keyword | Description |
|---|---|
multiprocessing.Process() | Creates a new process |
process.start() | Starts process execution |
process.join() | Waits for process completion |
multiprocessing.Pool() | Creates a pool of worker processes |
multiprocessing.Queue() | Shared queue for inter-process communication |
multiprocessing.Pipe() | Creates a pipe for process communication |
os.getpid() | Gets current process ID |
Async Programming (Event-Driven)
- Single-threaded
- Cooperative multitasking
- Concurrent Programming
| Keyword | Description |
|---|---|
async def | Defines a coroutine function |
await | Waits for coroutine completion |
asyncio.create_task() | Schedules a coroutine for execution |
asyncio.gather() | Runs multiple coroutines concurrently |
asyncio.run() | Runs the async program |
asyncio.sleep() | Non-blocking sleep operation |
asyncio.Queue() | Queue for async communication |
asyncio.Event() | Event synchronization between coroutines |
async without await: Your function won’t be recognized as a coroutine await without async: You’ll get a syntax error
git clone https://github.com/gchandra10/python_concurrent_pgmming_examples.git
Types of Tasks
| Task Type | Description | Best Handled By | Examples |
|---|---|---|---|
| CPU-Bound | Tasks that primarily involve processor and mathematical calculations | Multiprocessing | - Image processing - Machine learning computations - Complex calculations - Scientific simulations |
| I/O-Bound | Tasks that primarily wait for input/output operations to complete | Threading or Asyncio | - Database queries - File operations - Network requests - API calls |
| Memory-Bound | Tasks limited by memory access and transfer speed | Multiprocessing with shared memory | - Large dataset operations - Complex data structures - Memory-intensive algorithms |
| Cache-Bound | Tasks heavily dependent on CPU cache performance | Threading with cache alignment | - Matrix operations - Frequent data access - Array manipulations |
| Network-Bound | Tasks limited by network communication speed | Asyncio or Threading | - Web scraping - Streaming data - Real-time updates - Cloud operations |
| Disk-Bound | Tasks limited by disk read/write operations | Asyncio or Threading | - File reading/writing - Log processing - Database file operations |