Back to Python Course

Python Course

Processes vs Threads

Feature Process Thread ------------- ---------------------------------------- -------------------------------- Definition A self-contained execution environment A smaller unit of a process Memory Each process has its own memory Threads share memory space Communication Inter-process communication (IPC) needed Threads can communicate directly Overhead Higher Lower Use case CPU-bound tasks I/O-bound tasks

Example

  • Process: Running multiple Python scripts simultaneously.
  • Thread: Downloading files in the background while the main app is running.

Using Threads

Importing the threading Module.

import threading def print_numbers(): for i in range(5): print(f"Number: {i}") # Create thread t1 = threading.Thread(target=print_numbers) t1.start() print("Main thread continues...") t1.join()

Here if you see,

  • Thread(target=function) creates a thread.
  • start() begins thread execution.
  • join() waits for the thread to finish.

Let's see some use cases for Threading,

  • Reading multiple files from disk.
  • Fetching data from APIs concurrently.

Using Multiprocessing

Importing the multiprocessing Module.

import multiprocessing def square(n): print(f"Square of {n}: {n*n}") if __name__ == "__main__": p1 = multiprocessing.Process(target=square, args=(5,)) p1.start() p1.join()

Here if you see,

  • if __name__ == "__main__" is mandatory on Windows.
  • Each process runs independently with its own memory.

Let's see use case for Multiprocessing,

  • Performing heavy CPU computations (e.g., data processing, simulations).

Common Issues

Race Conditions (Threads)

When multiple threads modify shared data at the same time.

import threading counter = 0 def increment(): global counter for _ in range(1000000): counter += 1 threads = [threading.Thread(target=increment) for _ in range(2)] for t in threads: t.start() for t in threads: t.join() print(counter) # Output may not be 2,000,000

Fix: Use a Lock.

lock = threading.Lock() def increment(): global counter for _ in range(1000000): with lock: counter += 1

Deadlock

Occurs when two threads wait on each other indefinitely.

When to Use What?

Use Case Recommendation -------------------- ---------------------------- CPU-bound tasks Use multiprocessing I/O-bound tasks Use threading Shared data required Use threading with caution Performance critical Profile both