• trivial to run in parallel • no communication or synchronization needed • your typical web app Embarrassingly parallel problem Sunday, October 13, 13
• OS is the scheduler • preemptive multitasking • pros: simple • cons: takes a lot of RAM, you can’t really have “a lot” of processes Process based concurrency Sunday, October 13, 13
CPU bound program 2 Threads Thread(fib(25)) Thread(fib(25)) 1 Thread fib(35) fib(35) real 0m5.044s user 0m5.040s sys 0m0.000s real 0m7.462s user 0m8.980s sys 0m2.728s Sunday, October 13, 13
I/O bound program 3 Threads Thread(urlopen()) Thread(urlopen()) Thread(urlopen()) 1 Thread urlopen() urlopen() urlopen() real 0m0.375s user 0m0.028s sys 0m0.004s real 0m0.145s user 0m0.036s sys 0m0.000s Sunday, October 13, 13
• Python releases the GIL • OS makes the request and suspends the thread until the response is here When a thread does I/O call... Sunday, October 13, 13
• thread per request Problem is in the blocking thing. When our program does a blocking I/O call, OS scheduler suspends that thread and we can’t run anything else in that thread. Might not? Sunday, October 13, 13
We would like to be able to run multiple things inside a single OS thread (handle multiple requests). When one request makes a blocking I/O call, continue processing another request. Sunday, October 13, 13
• pros: you don’t have to change the flow of your program • cons: • because of the way it works, your program can’t make regular I/O calls • also hard to debug Coroutine pros & cons (seriously) Sunday, October 13, 13