Skip to content

Instantly share code, notes, and snippets.

@INF800
Last active September 12, 2021 23:33
Show Gist options
  • Save INF800/7b7086d09c8540f4b4f6f4630dccc1ee to your computer and use it in GitHub Desktop.
Save INF800/7b7086d09c8540f4b4f6f4630dccc1ee to your computer and use it in GitHub Desktop.
multiprocessing vs multithreading for cpu-bound and io-bound tasks
# source: https://www.geeksforgeeks.org/difference-between-multithreading-vs-multiprocessing-in-python/
import time, os
from threading import Thread, current_thread
from multiprocessing import Process, current_process
COUNT = 200000000
SLEEP = 10
def io_bound(sec):
"""in python, threading is ideal. Even if multiprocessing does it in same time, multiprocessing is resource intensive."""
pid = os.getpid()
threadName = current_thread().name
processName = current_process().name
print(f"{pid} * {processName} * {threadName} ---> Start sleeping...")
time.sleep(sec)
print(f"{pid} * {processName} * {threadName} ---> Finished sleeping...")
def cpu_bound(n):
"""in python, multiproc is ideal"""
pid = os.getpid()
threadName = current_thread().name
processName = current_process().name
print(f"{pid} * {processName} * {threadName} ---> Start counting...")
while n>0:
n -= 1
print(f"{pid} * {processName} * {threadName} ---> Finished counting...")
def test_code():
# 1. two consecutive io bound tasks
# =================================
# i. main thread on main process
# time: 20 sec
# io_bound(SLEEP)
# io_bound(SLEEP)
# ii. multiple threads on main processs
# time: 10 sec (speeds up io bound tasks. optimally utilizes wait time.)
# t1 = Thread(target=io_bound, args =(SLEEP, ))
# t2 = Thread(target=io_bound, args =(SLEEP, ))
# t1.start()
# t2.start()
# t1.join()
# t2.join()
# iii. multiple processes
# time: 10 sec (same but inefficent than multithreading, a process needs more resources compared to a thread)
p1 = Process(target = io_bound, args =(SLEEP, ))
p2 = Process(target = io_bound, args =(SLEEP, ))
p1.start()
p2.start()
p1.join()
p2.join()
# 1. two consecutive cpu bound tasks
# ==================================
# i. main thread on main process
# time: 19 sec
# cpu_bound(COUNT)
# cpu_bound(COUNT)
# ii. multiple threads on main processs
# time: 19 sec (there is no GIL lock as in blog! acc to blog, should be 40sec)
# t1 = Thread(target = cpu_bound, args =(COUNT, ))
# t2 = Thread(target = cpu_bound, args =(COUNT, ))
# t1.start()
# t2.start()
# t1.join()
# t2.join()
# iii. multiple process
# time: 10 sec (speeds up cou bound task)
# p1 = Process(target = cpu_bound, args =(COUNT, ))
# p2 = Process(target = cpu_bound, args =(COUNT, ))
# p1.start()
# p2.start()
# p1.join()
# p2.join()
if __name__=="__main__":
start = time.time()
test_code()
end = time.time()
print(f'Time taken in seconds: {end - start:.10f}')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment