Multi-Tasking in Python - Speed Up Your Program 10x by Executing Things Simultaneously - by Mike Huls - Towards Data Science
Multi-Tasking in Python - Speed Up Your Program 10x by Executing Things Simultaneously - by Mike Huls - Towards Data Science
You have 2 free member-only stories left this month. Sign up for Medium and get an extra one.
Member-only story
Listen Share
https://towardsdatascience.com/multi-tasking-in-python-speed-up-your-program-10x-by-executing-things-simultaneously-4b4fc7ee71e 1/18
6/20/23, 3:30 PM Multi-tasking in Python: Speed up your program 10x by executing things simultaneously | by Mike Huls | Towards Data Science
Before we begin I’d strongly suggest to check out the article below. It explains how
Python works under the hood and why it isn’t as fast as other languages. Also it
reveals why isn’t Python multi-threaded to begin with? You’ll have a better
understanding of what the problem we’re trying to solve in this article. Let’s code!
1. toast bread
2. boil water
3. boil egg
https://towardsdatascience.com/multi-tasking-in-python-speed-up-your-program-10x-by-executing-things-simultaneously-4b4fc7ee71e 2/18
6/20/23, 3:30 PM Multi-tasking in Python: Speed up your program 10x by executing things simultaneously | by Mike Huls | Towards Data Science
This is what we’re trying to make in as little time as possible (image by Eiliv-Sonas Aceron on Unsplash)
How would you go about this? One way is to perform each task sequentially; first
toast bread, then boil water and an egg an then switch on the coffee maker.
Although this process is pretty understandable, in the end it just leaves us with some
cold toast, a cold egg and a hot cup of coffee. Alternatively we could perform some
tasks simultaneously; we’ll switch on the coffee maker and toaster and boil some
water at the same time.
https://towardsdatascience.com/multi-tasking-in-python-speed-up-your-program-10x-by-executing-things-simultaneously-4b4fc7ee71e 3/18
6/20/23, 3:30 PM Multi-tasking in Python: Speed up your program 10x by executing things simultaneously | by Mike Huls | Towards Data Science
1 def toast_bread():
2 print("toasting bread..")
To make Medium work, we log user data.
3 time.sleep(8) By using Medium, you agree to our
4 Privacy Policy, including cookie policy.
print("bread toasted")
5
6 def make_some_coffee():
7 print("turned on coffee maker..")
8 time.sleep(4)
9 print("Poured a nice cup of coffee")
10
11 def boil_water_and_egg():
12 print("boiling water..")
13 time.sleep(5.5)
14 print("water boiled")
We’ll run this code sequentially (one after the other) like this:
toast_bread()
boil_water_and_egg()
make_some_coffee()
Making breakfast sequentially will take around 17.5 seconds. This involves a lot of
waiting! Let’s multitask using threads:
The code is pretty straight-forward: we’ll create some tasks and append all of them
to a list. Then we’ll start each thread in the list and wait for all of them to finish (this
https://towardsdatascience.com/multi-tasking-in-python-speed-up-your-program-10x-by-executing-things-simultaneously-4b4fc7ee71e 4/18
6/20/23, 3:30 PM Multi-tasking in Python: Speed up your program 10x by executing things simultaneously | by Mike Huls | Towards Data Science
The main take-away is that if there’s a lot of waiting involved (typical in I/O tasks like
downloading data, API requests, writing files..) we can use threads to multi-task.
Later on in this article we’ll examine why threads are the best option for I/O-tasks.
https://towardsdatascience.com/multi-tasking-in-python-speed-up-your-program-10x-by-executing-things-simultaneously-4b4fc7ee71e 5/18
6/20/23, 3:30 PM Multi-tasking in Python: Speed up your program 10x by executing things simultaneously | by Mike Huls | Towards Data Science
Let’s first translate our homework example to some code. We’ll simulate doing our
homework with some CPU-intensive processing; adding up all numbers from 0 to
100 million:
1 def do_math_homework():
2
3 res = 0
4 for i in range(100_000_000):
5 res += i
6 return res
7
8 def do_physics_homework(num_of_tasks:int):
9 """same as do_math_homework)"""
10 def do_chemistry_homework(num_of_tasks:int):
11 """same as do_math_homework)"""
previous part) and then using the code below to spawn processes. Notice that the
To make Medium work,
code looks a lot like the threading-code fromwe log
theuser data.
previous part.
By using Medium, you agree to our
Privacy Policy, including cookie policy.
1 from multiprocessing import Process
2
3 processlist = []
4
5 processlist.append(Process(target=do_math_homework)))
6 processlist.append(Process(target=do_physics_homework))
7 processlist.append(Process(target=do_chemistry_homework))
8
9 for t in processlist:
10 t.start()
11
12 for t in processlist:
13 t.join()
The benchmaks:
You’ll see that using processes speeds up executing by quite a lot! The reason for this
is that we can use more CPU’s.
Summary
Threads are for making-breakfast-like tasks: it involves a lot of waiting so one
‘person’ (or CPU) can do thing simultaneously. Processes are for ‘thinking-tasks’;
they need you to be there to do the heavy work. Multiprocessing is like creating a
clone of yourself so it can do other things while you are working on your task.
https://towardsdatascience.com/multi-tasking-in-python-speed-up-your-program-10x-by-executing-things-simultaneously-4b4fc7ee71e 7/18
6/20/23, 3:30 PM Multi-tasking in Python: Speed up your program 10x by executing things simultaneously | by Mike Huls | Towards Data Science
Let’s find out how they run (image by Erik Mclean on Unsplash)
https://towardsdatascience.com/multi-tasking-in-python-speed-up-your-program-10x-by-executing-things-simultaneously-4b4fc7ee71e 8/18
6/20/23, 3:30 PM Multi-tasking in Python: Speed up your program 10x by executing things simultaneously | by Mike Huls | Towards Data Science
The main one is that processes can’t share resources while threads can. This is
because a process works To make
with Medium work,
multiple CPU’swe log user data.a thread is just one CPU going
whereas
By using Medium, you agree to our
back and forth between multiple threads.
Privacy Policy, including cookie policy.
You can think of threading as a single CPU that first executes a few lines of code in
thread1, then it executes some lines in thread2, then moves on to thread3. Then it
executes the next line in thread1, then thread2 etc. Threading executes multiple
tasks concurrently; one worker that switches between tasks. For the user it looks as
though things happen simultaneously, this isn’t technically so.
When you spawn a new process a whole new instance of python is created and
allocated to a different CPU. This is the reason why two processes cannot share a
common resource. Processes run in parallel; there are multiple workers that work
on multiple tasks simultaneously.
Overhead
Processes take a little more time to spawn. This is the reason the homework
example is not three times faster but slightly less; first we’ll have to spawn the
processes before we can benefit from the parallelism.
Cython for absolute beginners: 30x faster code in two simple steps
Easy Python code compilation for blazingly fast applications
towardsdatascience.com
Conclusion
https://towardsdatascience.com/multi-tasking-in-python-speed-up-your-program-10x-by-executing-things-simultaneously-4b4fc7ee71e 9/18
6/20/23, 3:30 PM Multi-tasking in Python: Speed up your program 10x by executing things simultaneously | by Mike Huls | Towards Data Science
Threading and multiprocessing can be used to speed up the execution of your code
To make
in many, many cases. In this Medium
article work, explored
we’ve we log user data.
what threads and processes are,
By using Medium, you agree to our
how they work and whenPrivacy
to usePolicy,
which. Don’t forget to check out this article on how
including cookie policy.
to apply pools and for benchmarks!
Getting started with Cython: how to perform >1.7 billion calculations per second
in Python
Create Your Custom, private Python Package That You Can PIP Install From Your
Git Repository
Virtual environments for absolute beginners — what is it and how to create one
(+ examples)
Happy coding!
— Mike
Follow
I'm a full-stack developer with a passion for programming, technology and traveling. — mikehuls.com —
https://mikehuls.medium.com/membership
https://towardsdatascience.com/multi-tasking-in-python-speed-up-your-program-10x-by-executing-things-simultaneously-4b4fc7ee71e 11/18
6/20/23, 3:30 PM Multi-tasking in Python: Speed up your program 10x by executing things simultaneously | by Mike Huls | Towards Data Science
210 2
4K 50
https://towardsdatascience.com/multi-tasking-in-python-speed-up-your-program-10x-by-executing-things-simultaneously-4b4fc7ee71e 12/18
6/20/23, 3:30 PM Multi-tasking in Python: Speed up your program 10x by executing things simultaneously | by Mike Huls | Towards Data Science
Stop Hard Coding in a Data Science Project — Use Config Files Instead
And How to Efficiently Interact with Config Files in Python
1.5K 19
https://towardsdatascience.com/multi-tasking-in-python-speed-up-your-program-10x-by-executing-things-simultaneously-4b4fc7ee71e 13/18
6/20/23, 3:30 PM Multi-tasking in Python: Speed up your program 10x by executing things simultaneously | by Mike Huls | Towards Data Science
326 1
https://towardsdatascience.com/multi-tasking-in-python-speed-up-your-program-10x-by-executing-things-simultaneously-4b4fc7ee71e 14/18
6/20/23, 3:30 PM Multi-tasking in Python: Speed up your program 10x by executing things simultaneously | by Mike Huls | Towards Data Science
157 3
Lists
New_Reading_List
173 stories
https://towardsdatascience.com/multi-tasking-in-python-speed-up-your-program-10x-by-executing-things-simultaneously-4b4fc7ee71e 15/18
6/20/23, 3:30 PM Multi-tasking in Python: Speed up your program 10x by executing things simultaneously | by Mike Huls | Towards Data Science
181 4
https://towardsdatascience.com/multi-tasking-in-python-speed-up-your-program-10x-by-executing-things-simultaneously-4b4fc7ee71e 16/18
6/20/23, 3:30 PM Multi-tasking in Python: Speed up your program 10x by executing things simultaneously | by Mike Huls | Towards Data Science
1.98K 21
https://towardsdatascience.com/multi-tasking-in-python-speed-up-your-program-10x-by-executing-things-simultaneously-4b4fc7ee71e 17/18
6/20/23, 3:30 PM Multi-tasking in Python: Speed up your program 10x by executing things simultaneously | by Mike Huls | Towards Data Science
159
https://towardsdatascience.com/multi-tasking-in-python-speed-up-your-program-10x-by-executing-things-simultaneously-4b4fc7ee71e 18/18