Upgrade to Pro — share decks privately, control downloads, hide ads and more …

On Concurrency

On Concurrency

On Concurrency talk at Pycon Ireland 2013

Goran Peretin

October 13, 2013

More Decks by Goran Peretin

Other Decks in Programming


  1. • explanations of how something works • understand terms and

    concepts There will be... Sunday, October 13, 13
  2. • turnkey solutions • pip install concurrency (?) • inner

    workings of these libraries There won’t be... Sunday, October 13, 13
  3. Executing multiple tasks in the same time frame. What is

    concurrency? Sunday, October 13, 13
  4. Parallelism means executing simultaneously. We need multiple CPUs for that.

    Concurrency != Parallelism Sunday, October 13, 13
  5. • trivial to run in parallel • no communication or

    synchronization needed • your typical web app Embarrassingly parallel problem Sunday, October 13, 13
  6. • 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
  7. • OS is the scheduler • pros: takes less RAM,

    still simple • cons: well... Thread based concurrency Sunday, October 13, 13
  8. Only one thread can run Python program at any point

    in time. Threads can’t run in parallel. Global Interpreter Lock Sunday, October 13, 13
  9. When can a thread run Python? • OS scheduler schedules

    that thread • Thread manages to acquire the GIL Sunday, October 13, 13
  10. CPU bound - uses a lot of CPU IO bound

    - makes a lot of I/O requests CPU bound vs IO bound task Sunday, October 13, 13
  11. 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
  12. Let’s focus on I/O bound tasks • if we have

    CPU bound task, we have to use multiple processes Sunday, October 13, 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
  14. • 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
  15. • 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
  16. 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
  17. • pass in callback function with the I/O call •

    pros: it’s not a hack • cons: it’s callback based Callback-based Sunday, October 13, 13
  18. • use coroutines as microthreads • pros: it’s not callback

    based • cons: it’s a hack Coroutine-based Sunday, October 13, 13
  19. Function that can suspend it’s execution and then later resume

    where it was suspended. Greenlets. Coroutine Sunday, October 13, 13
  20. • 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
  21. Just tell me what should I pip install to make

    it work... So... Sunday, October 13, 13
  22. If you really need *high* concurrency, Python probably isn’t the

    tool for the job. <remove this slide> Sunday, October 13, 13