Threading vs Multiprocessing in Python

# threading example
from threading import Thread
import os
import math

def calc():
for i in range(0, 4000000):
math.sqrt(i)

threads = []

for i in range(os.cpu_count()):
print('registering thread %d' % i)
threads.append(Thread(target=calc))

for thread in threads:
thread.start()

for thread in threads:
thread.join()
One thread even though we created multiple ones
# multi processing example
from multiprocessing import Process
import os
import math

def calc():
for i in range(0, 70000000):
math.sqrt(i)

processes = []

for i in range(os.cpu_count()):
print('registering process %d' % i)
processes.append(Process(target=calc))

for process in processes:
process.start()

for process in processes:
process.join()
Each with their own process

--

--

--

Reinforcement Learning , GAN & CNN. Occasionally embedded software development stuff

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Launching AWS EC2 Instance and Configuring Webserver automatically using Ansible

Testing your C# code with xUnit

Reliable Enterprise Low-code consultancy in Vietnam

Software Development: A Woodworkers Perspective (Part 3)

How to Deploy .NET

Main App Bug Bounty Methodology —  V3

DevOps is a culture that combines two ends of the SDLC lifecycle, the Developers and the Operations.

Compliance and delegation of Azure Locks through Azure Policy

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Ugurkan Ates

Ugurkan Ates

Reinforcement Learning , GAN & CNN. Occasionally embedded software development stuff

More from Medium

What Programming languages to learn first?(Its Not Python)

Cell Lifecycle at a given time in Python and Golang

How to reverse parametrize in PyTest fixtures

Indirect parametrize

How to compare and find difference between two Json object in python