intro to multitasking
TRANSCRIPT
![Page 1: Intro to Multitasking](https://reader030.vdocuments.mx/reader030/viewer/2022020213/587d37751a28ab2a448b6321/html5/thumbnails/1.jpg)
![Page 3: Intro to Multitasking](https://reader030.vdocuments.mx/reader030/viewer/2022020213/587d37751a28ab2a448b6321/html5/thumbnails/3.jpg)
intro to multitasking(with Python)
![Page 4: Intro to Multitasking](https://reader030.vdocuments.mx/reader030/viewer/2022020213/587d37751a28ab2a448b6321/html5/thumbnails/4.jpg)
what is multitasking?
![Page 5: Intro to Multitasking](https://reader030.vdocuments.mx/reader030/viewer/2022020213/587d37751a28ab2a448b6321/html5/thumbnails/5.jpg)
quick diversion: processors
![Page 6: Intro to Multitasking](https://reader030.vdocuments.mx/reader030/viewer/2022020213/587d37751a28ab2a448b6321/html5/thumbnails/6.jpg)
• this is Apple’s A8 CPU
• it has CPU 2 cores
• each core can do 1 thing at once
• switches between tasks when there’s downtime
• "preemptive multitasking"
processors
![Page 7: Intro to Multitasking](https://reader030.vdocuments.mx/reader030/viewer/2022020213/587d37751a28ab2a448b6321/html5/thumbnails/7.jpg)
quick diversion: operating systems
![Page 8: Intro to Multitasking](https://reader030.vdocuments.mx/reader030/viewer/2022020213/587d37751a28ab2a448b6321/html5/thumbnails/8.jpg)
some OS terminology• process
• a running program
• isolated memory (it can’t see another process’ memory)
• all processes require their own memory
• starting and stopping processes can take time
![Page 9: Intro to Multitasking](https://reader030.vdocuments.mx/reader030/viewer/2022020213/587d37751a28ab2a448b6321/html5/thumbnails/9.jpg)
![Page 10: Intro to Multitasking](https://reader030.vdocuments.mx/reader030/viewer/2022020213/587d37751a28ab2a448b6321/html5/thumbnails/10.jpg)
• thread
• a series of instructions that can be executed asynchronously inside a process
• a process can have multiple threads
• every thread must belong to a process
• threads all share memory with their parent process
• quick to start up, easy to shut down
some OS terminology
![Page 11: Intro to Multitasking](https://reader030.vdocuments.mx/reader030/viewer/2022020213/587d37751a28ab2a448b6321/html5/thumbnails/11.jpg)
what does this mean to me?
• processes and threads are the building blocks for multitasking
• it’s important to know the differences and tradeoffs for each
![Page 12: Intro to Multitasking](https://reader030.vdocuments.mx/reader030/viewer/2022020213/587d37751a28ab2a448b6321/html5/thumbnails/12.jpg)
let’s do some multitasking
![Page 13: Intro to Multitasking](https://reader030.vdocuments.mx/reader030/viewer/2022020213/587d37751a28ab2a448b6321/html5/thumbnails/13.jpg)
the problem
• you’re building a program to fetch a bunch of wikipedia articles (~1000)
![Page 14: Intro to Multitasking](https://reader030.vdocuments.mx/reader030/viewer/2022020213/587d37751a28ab2a448b6321/html5/thumbnails/14.jpg)
the solution (no multitasking)import requests
links = [] with open('articles.txt') as f: links = f.readlines()
articles = [] for link in links: response = requests.get(link) articles.append(response.text)
time: ~5m 54s
![Page 15: Intro to Multitasking](https://reader030.vdocuments.mx/reader030/viewer/2022020213/587d37751a28ab2a448b6321/html5/thumbnails/15.jpg)
look at all that cpu we aren’t using
![Page 16: Intro to Multitasking](https://reader030.vdocuments.mx/reader030/viewer/2022020213/587d37751a28ab2a448b6321/html5/thumbnails/16.jpg)
the waiting is the hardest part
http://blog.codinghorror.com/the-infinite-space-between-words/
![Page 17: Intro to Multitasking](https://reader030.vdocuments.mx/reader030/viewer/2022020213/587d37751a28ab2a448b6321/html5/thumbnails/17.jpg)
the solution (threads)import requests from multiprocessing.pool import ThreadPool
links = [] with open('articles.txt') as f: links = f.readlines()
def do_get(link): response = requests.get(link) return response.text
with ThreadPool(4) as p: articles = p.map(do_get, links)
time: ~1m 43s
![Page 18: Intro to Multitasking](https://reader030.vdocuments.mx/reader030/viewer/2022020213/587d37751a28ab2a448b6321/html5/thumbnails/18.jpg)
cpu usage is better this time
![Page 19: Intro to Multitasking](https://reader030.vdocuments.mx/reader030/viewer/2022020213/587d37751a28ab2a448b6321/html5/thumbnails/19.jpg)
the solution (processes)import requests from multiprocessing.pool import Pool
links = [] with open('articles.txt') as f: links = f.readlines()
def do_get(link): response = requests.get(link) return response.text
with Pool(4) as p: articles = p.map(do_get, links)
time: ~1m 43s
![Page 20: Intro to Multitasking](https://reader030.vdocuments.mx/reader030/viewer/2022020213/587d37751a28ab2a448b6321/html5/thumbnails/20.jpg)
lots of processes this time
![Page 21: Intro to Multitasking](https://reader030.vdocuments.mx/reader030/viewer/2022020213/587d37751a28ab2a448b6321/html5/thumbnails/21.jpg)
it’s faster!
![Page 22: Intro to Multitasking](https://reader030.vdocuments.mx/reader030/viewer/2022020213/587d37751a28ab2a448b6321/html5/thumbnails/22.jpg)
why?
• because the CPU can spend more time actually doing something instead of waiting around
• threads: whenever there’s downtime, it switches to a different thread
• processes: whenever it can, the processors share the workload
![Page 23: Intro to Multitasking](https://reader030.vdocuments.mx/reader030/viewer/2022020213/587d37751a28ab2a448b6321/html5/thumbnails/23.jpg)
![Page 24: Intro to Multitasking](https://reader030.vdocuments.mx/reader030/viewer/2022020213/587d37751a28ab2a448b6321/html5/thumbnails/24.jpg)
quick samples in other languages
![Page 25: Intro to Multitasking](https://reader030.vdocuments.mx/reader030/viewer/2022020213/587d37751a28ab2a448b6321/html5/thumbnails/25.jpg)
ExecutorService executorService = Executors.newThreadExecutor();
executorService.execute(new Runnable() { public void run() { //Do your heavy lifting here. } }); executorService.shutdown();
threads in Java
![Page 26: Intro to Multitasking](https://reader030.vdocuments.mx/reader030/viewer/2022020213/587d37751a28ab2a448b6321/html5/thumbnails/26.jpg)
threads in Objective-C
dispatch_async( dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void){
//Do your heavy lifting here. });
![Page 27: Intro to Multitasking](https://reader030.vdocuments.mx/reader030/viewer/2022020213/587d37751a28ab2a448b6321/html5/thumbnails/27.jpg)
threads in good ol’ C#include <stdio.h> #include <pthread.h>
void *doSomething(void *arg) { // Do your heavy lifting here. }
int main(void) { pthread_t pth; // this is our thread identifier /* Create worker thread */ pthread_create(&pth,NULL,doSomething,"processing..."); /* wait for our thread to finish */ pthread_join(pth, NULL); return 0; }
![Page 28: Intro to Multitasking](https://reader030.vdocuments.mx/reader030/viewer/2022020213/587d37751a28ab2a448b6321/html5/thumbnails/28.jpg)
wait… one question
![Page 29: Intro to Multitasking](https://reader030.vdocuments.mx/reader030/viewer/2022020213/587d37751a28ab2a448b6321/html5/thumbnails/29.jpg)
question
• in the examples both multithreading and multiprocessing took basically the same amount of time
• they were both ~4-5x faster than the normal case
• why use one over the other?
![Page 30: Intro to Multitasking](https://reader030.vdocuments.mx/reader030/viewer/2022020213/587d37751a28ab2a448b6321/html5/thumbnails/30.jpg)
answer: context
![Page 31: Intro to Multitasking](https://reader030.vdocuments.mx/reader030/viewer/2022020213/587d37751a28ab2a448b6321/html5/thumbnails/31.jpg)
good candidates
• lots of simple, or independent tasks
• no shared state
• I/O bound tasks are easier
• CPU bound tasks are harder*
![Page 32: Intro to Multitasking](https://reader030.vdocuments.mx/reader030/viewer/2022020213/587d37751a28ab2a448b6321/html5/thumbnails/32.jpg)
more examples
![Page 33: Intro to Multitasking](https://reader030.vdocuments.mx/reader030/viewer/2022020213/587d37751a28ab2a448b6321/html5/thumbnails/33.jpg)
the problem
• you’re building a program that can take a given number and sum all it’s previous values
![Page 34: Intro to Multitasking](https://reader030.vdocuments.mx/reader030/viewer/2022020213/587d37751a28ab2a448b6321/html5/thumbnails/34.jpg)
the solution (no multitasking)def do_something(n): val = 0 for i in range(n): for x in range(i): val += x return val
do_something(2000) do_something(15000) do_something(10000) do_something(1000) do_something(12000)
time: ~19s
![Page 35: Intro to Multitasking](https://reader030.vdocuments.mx/reader030/viewer/2022020213/587d37751a28ab2a448b6321/html5/thumbnails/35.jpg)
1 cpu: 99% used
![Page 36: Intro to Multitasking](https://reader030.vdocuments.mx/reader030/viewer/2022020213/587d37751a28ab2a448b6321/html5/thumbnails/36.jpg)
from multiprocessing.pool import Pool
def do_something(n): val = 0 for i in range(n): for x in range(i): val += x return val
with Pool(4) as p: nums = [2000, 15000, 10000, 1000, 12000] p.map(do_something, nums)
the solution (processes)
time: ~13s
![Page 37: Intro to Multitasking](https://reader030.vdocuments.mx/reader030/viewer/2022020213/587d37751a28ab2a448b6321/html5/thumbnails/37.jpg)
use all the CPUs!
![Page 38: Intro to Multitasking](https://reader030.vdocuments.mx/reader030/viewer/2022020213/587d37751a28ab2a448b6321/html5/thumbnails/38.jpg)
the solution (threads)remember that catch I talked about? here it is.
from multiprocessing.pool import ThreadPool
def do_something(n): val = 0 for i in range(n): for x in range(i): val += x return val
with ThreadPool(4) as p: nums = [2000, 15000, 10000, 1000, 12000] p.map(do_something, nums)
time: ~20s
![Page 39: Intro to Multitasking](https://reader030.vdocuments.mx/reader030/viewer/2022020213/587d37751a28ab2a448b6321/html5/thumbnails/39.jpg)
why did this happen?
![Page 40: Intro to Multitasking](https://reader030.vdocuments.mx/reader030/viewer/2022020213/587d37751a28ab2a448b6321/html5/thumbnails/40.jpg)
Python does threads different
• the quick, quick, quick version:
• Python threads are real OS threads
• but only 1 can execute at a time
• result:
• using threads for CPU bound tasks is slower
WARNING: this is specific to Python!
![Page 41: Intro to Multitasking](https://reader030.vdocuments.mx/reader030/viewer/2022020213/587d37751a28ab2a448b6321/html5/thumbnails/41.jpg)
it pays to be familiar with your tools
![Page 42: Intro to Multitasking](https://reader030.vdocuments.mx/reader030/viewer/2022020213/587d37751a28ab2a448b6321/html5/thumbnails/42.jpg)
some other types of multitasking
![Page 43: Intro to Multitasking](https://reader030.vdocuments.mx/reader030/viewer/2022020213/587d37751a28ab2a448b6321/html5/thumbnails/43.jpg)
coroutines• 1 thread but with code that is good to it’s
neighbors
• functions can pause, and when they do, the program switches to another function for a bit
• useful in situations where lots of waiting is involved (i.e. networking)
• example: Python’s gevent or async syntax
![Page 44: Intro to Multitasking](https://reader030.vdocuments.mx/reader030/viewer/2022020213/587d37751a28ab2a448b6321/html5/thumbnails/44.jpg)
asynchronous functions
• single threaded
• every function is treated as asynchronous. there is no such thing as synchronous code.
• example: Javascript… all of it.
![Page 45: Intro to Multitasking](https://reader030.vdocuments.mx/reader030/viewer/2022020213/587d37751a28ab2a448b6321/html5/thumbnails/45.jpg)
stuff I didn’t cover
• semaphores
• locks
• race-conditions
• basically all the scary stuff
![Page 46: Intro to Multitasking](https://reader030.vdocuments.mx/reader030/viewer/2022020213/587d37751a28ab2a448b6321/html5/thumbnails/46.jpg)
multitasking done right can be really helpful; when done wrong it can be disastrous
![Page 47: Intro to Multitasking](https://reader030.vdocuments.mx/reader030/viewer/2022020213/587d37751a28ab2a448b6321/html5/thumbnails/47.jpg)
and use Python 3 - it’s easier there
go out and do it!