DEV Community

Aruna Arun
Aruna Arun

Posted on

Day 20: Python Programming

Multithreading & Multiprocessing

Topics Covered

  1. Process vs Thread
  2. Multithreading
  3. Thread class
  4. Synchronization (Lock)
  5. Multiprocessing
  6. CPU-bound vs I/O-bound
  7. Real-time & DevOps use cases

1. Process vs Thread
Process

  • Independent program
  • Separate memory
  • Slower creation
  • Uses CPU heavily

Thread

  • Part of a process
  • Shared memory
  • Faster execution
  • Lightweight

One process can have multiple threads

2. CPU-bound vs I/O-bound Tasks
I/O-bound (Best for Multithreading)

  • File read/write
  • API calls
  • Network operations
  • Sleep

CPU-bound (Best for Multiprocessing)

  • Calculations
  • Image processing
  • ML tasks

3. Multithreading
Used to run multiple tasks concurrently.
Python module:
import threading

4. Create a Thread (Basic Example)
import threading
def task():
print("Thread task running")
t = threading.Thread(target=task)
t.start()

5. Multiple Threads Example
import threading
def job(name):
print("Task:", name)
t1 = threading.Thread(target=job, args=("A",))
t2 = threading.Thread(target=job, args=("B",))
t1.start()
t2.start()

6. Thread with Loop
import threading
def numbers():
for i in range(5):
print(i)
t = threading.Thread(target=numbers)
t.start()

7. Join Threads
join() waits for thread to finish.
t1.start()
t1.join()
print("Main program ends")

8. Multithreading Example (I/O-Bound)
import threading
import time
def download(file):
print("Downloading", file)
time.sleep(2)
print("Completed", file)
files = ["A", "B", "C"]
threads = []
for f in files:
t = threading.Thread(target=download, args=(f,))
t.start()
threads.append(t)
for t in threads:
t.join()
print("All downloads complete")

9. Thread Synchronization (Lock)
Used to avoid data inconsistency.
import threading
lock = threading.Lock()
count = 0
def increment():
global count
for i in range(1000):
lock.acquire()
count += 1
lock.release()
t1 = threading.Thread(target=increment)
t2 = threading.Thread(target=increment)
t1.start()
t2.start()
t1.join()
t2.join()
print(count)

10. Why Lock is Required?
*Without lock → Race condition
*With lock → Safe shared data access

11. Multiprocessing
Uses multiple CPU cores.
Python module:
import multiprocessing

12. Multiprocessing Example
from multiprocessing import Process
def task():
print("Process running")
p = Process(target=task)
p.start()

13. Multiple Processes Example
from multiprocessing import Process
import os
def work():
print("Process ID:", os.getpid())
p1 = Process(target=work)
p2 = Process(target=work)
p1.start()
p2.start()

14. Multiprocessing with Join
p1.start()
p2.start()
p1.join()
p2.join()
print("Main process completed")

15. When NOT to Use Multithreading?
CPU-intensive tasks
*Heavy mathematical calculation
➡ Python has **GIL (Global Interpreter Lock)
*
➡ Blocks true parallel CPU execution

16. Multithreading vs Multiprocessing

Feature Multithreading Multiprocessing
Memory Shared Separate
Speed Fast I/O Fast CPU
GIL Affected Not affected
Use case I/O tasks CPU tasks

17. Real-Time DevOps Use Case
Running multiple server health checks
import threading
import time
def check_server(server):
print("Checking", server)
time.sleep(1)
print(server, "UP")
servers = ["Server1", "Server2", "Server3"]
for s in servers:
threading.Thread(target=check_server, args=(s,)).start()

Top comments (0)