Top Banner
48

Intro to Multitasking

Jan 17, 2017

Download

Software

Brian Schrader
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Intro to Multitasking
Page 3: Intro to Multitasking

intro to multitasking(with Python)

Page 4: Intro to Multitasking

what is multitasking?

Page 5: Intro to Multitasking

quick diversion: processors

Page 6: Intro to Multitasking

• 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

quick diversion: operating systems

Page 8: Intro to Multitasking

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
Page 10: Intro to Multitasking

• 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

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

let’s do some multitasking

Page 13: Intro to Multitasking

the problem

• you’re building a program to fetch a bunch of wikipedia articles (~1000)

Page 14: Intro to Multitasking

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

look at all that cpu we aren’t using

Page 16: Intro to Multitasking

the waiting is the hardest part

http://blog.codinghorror.com/the-infinite-space-between-words/

Page 17: Intro to Multitasking

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

cpu usage is better this time

Page 19: Intro to Multitasking

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

lots of processes this time

Page 21: Intro to Multitasking

it’s faster!

Page 22: Intro to Multitasking

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
Page 24: Intro to Multitasking

quick samples in other languages

Page 25: Intro to Multitasking

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

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

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

wait… one question

Page 29: Intro to Multitasking

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

answer: context

Page 31: Intro to Multitasking

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

more examples

Page 33: Intro to Multitasking

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

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

1 cpu: 99% used

Page 36: Intro to Multitasking

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

use all the CPUs!

Page 38: Intro to Multitasking

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

why did this happen?

Page 40: Intro to Multitasking

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

it pays to be familiar with your tools

Page 42: Intro to Multitasking

some other types of multitasking

Page 43: Intro to Multitasking

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

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

stuff I didn’t cover

• semaphores

• locks

• race-conditions

• basically all the scary stuff

Page 46: Intro to Multitasking

multitasking done right can be really helpful; when done wrong it can be disastrous

Page 47: Intro to Multitasking

and use Python 3 - it’s easier there

go out and do it!