Sobes.tech
Middle

What is parallelism?

sobes.tech AI

Answer from AI

Parallelism is the ability of a system to perform multiple tasks simultaneously or seemingly simultaneously. In Python, this is achieved using the threading and multiprocessing modules.

Key aspects:

  • Concurrent execution: Tasks are executed at the same time (in the case of multiprocessing on multi-core processors) or are switched between tasks so quickly that it creates an illusion of simultaneity (in the case of multithreading).
  • GIL (Global Interpreter Lock): In CPython, GIL limits the actual parallel execution of threads that use CPU-bound operations. For such tasks, multiprocessing is preferable.
  • Use cases: Parallelism is useful for I/O-bound tasks (network, file system), where threads can wait for operations to complete, and for CPU-bound tasks (computations), where processes can run on different cores.

Multithreading:

Threads share the same memory address space.

import threading
import time

def worker(num):
    """Thread worker function"""
    print(f'Worker: {num}')
    time.sleep(1)

threads = []
for i in range(5):
    t = threading.Thread(target=worker, args=(i,))  # Creating a thread
    threads.append(t)
    t.start()  # Starting the thread

for t in threads:
    t.join()  # Waiting for thread to finish

print('All threads finished.')

Multiprocessing:

Processes have separate memory address spaces.

import multiprocessing
import time

def worker(num):
    """Process worker function"""
    print(f'Worker: {num}')
    time.sleep(1)

processes = []
for i in range(5):
    p = multiprocessing.Process(target=worker, args=(i,))  # Creating a process
    processes.append(p)
    p.start()  # Starting the process

for p in processes:
    p.join()  # Waiting for process to finish

print('All processes finished.')

Differences between multithreading and multiprocessing:

Feature Multithreading Multiprocessing
Address space Shared Separate
GIL Affects CPU-bound tasks in CPython Does not affect, as each process has its own GIL
Creation Faster Slower (creating a new process)
Communication Via shared variables (requires synchronization) Via queues, pipes, managers (more secure)