$30 off During Our Annual Pro Sale. View Details »

On Concurrency

On Concurrency

On Concurrency talk at Pycon Ireland 2013

Goran Peretin

October 13, 2013
Tweet

More Decks by Goran Peretin

Other Decks in Programming

Transcript

  1. On Concurrency
    Sunday, October 13, 13

    View Slide

  2. Demo time!
    Sunday, October 13, 13

    View Slide

  3. Goran Peretin
    @gperetin
    Freelancer
    Hi!
    Sunday, October 13, 13

    View Slide

  4. Couple of notes...
    • Python -> cPython
    • Unix/Linux systems
    Sunday, October 13, 13

    View Slide

  5. • explanations of how something works
    • understand terms and concepts
    There will be...
    Sunday, October 13, 13

    View Slide

  6. • turnkey solutions
    • pip install concurrency (?)
    • inner workings of these libraries
    There won’t be...
    Sunday, October 13, 13

    View Slide

  7. Executing multiple tasks in the same time frame.
    What is concurrency?
    Sunday, October 13, 13

    View Slide

  8. Parallelism means executing simultaneously.
    We need multiple CPUs for that.
    Concurrency != Parallelism
    Sunday, October 13, 13

    View Slide

  9. • trivial to run in parallel
    • no communication or synchronization needed
    • your typical web app
    Embarrassingly parallel problem
    Sunday, October 13, 13

    View Slide

  10. Types of concurrency
    Sunday, October 13, 13

    View Slide

  11. Process based concurrency
    if task == process:
    Sunday, October 13, 13

    View Slide

  12. • 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

    View Slide

  13. Thread based concurrency
    if task == thread:
    Sunday, October 13, 13

    View Slide

  14. • OS is the scheduler
    • pros: takes less RAM, still simple
    • cons: well...
    Thread based concurrency
    Sunday, October 13, 13

    View Slide

  15. When we talk about threads and Python...
    Sunday, October 13, 13

    View Slide

  16. GIL
    Sunday, October 13, 13

    View Slide

  17. 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

    View Slide

  18. When can a thread run Python?
    • OS scheduler schedules that thread
    • Thread manages to acquire the GIL
    Sunday, October 13, 13

    View Slide

  19. 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

    View Slide

  20. CPU bound vs IO bound task
    Sunday, October 13, 13

    View Slide

  21. How is this related to GIL?
    Sunday, October 13, 13

    View Slide

  22. 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

    View Slide

  23. Sunday, October 13, 13

    View Slide

  24. Sunday, October 13, 13

    View Slide

  25. Yes, GIL is a problem for CPU bound tasks.
    Sunday, October 13, 13

    View Slide

  26. Let’s focus on I/O bound tasks
    • if we have CPU bound task, we have to use
    multiple processes
    Sunday, October 13, 13

    View Slide

  27. 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

    View Slide

  28. • 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

    View Slide

  29. Blocking I/O
    Sunday, October 13, 13

    View Slide

  30. GIL might not be a problem for I/O bound tasks.
    Sunday, October 13, 13

    View Slide

  31. • 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

    View Slide

  32. *A lot* of requests/tasks.
    Problem?
    Sunday, October 13, 13

    View Slide

  33. 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

    View Slide

  34. Non-blocking I/O
    Sunday, October 13, 13

    View Slide

  35. Non-blocking I/O
    Let’s do an I/O call that returns immediately.
    Sunday, October 13, 13

    View Slide

  36. Event-driven concurrency
    Non-blocking I/O gives us...
    Sunday, October 13, 13

    View Slide

  37. Callbacks (Twisted, Tornado)
    vs
    Coroutines (Gevent, Eventlet)
    Event-driven concurrency - Python
    Sunday, October 13, 13

    View Slide

  38. • 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

    View Slide

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

    View Slide

  40. Function that can suspend it’s execution and then later
    resume where it was suspended.
    Greenlets.
    Coroutine
    Sunday, October 13, 13

    View Slide

  41. • 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

    View Slide

  42. Callback or greenlets?
    Sunday, October 13, 13

    View Slide

  43. PEP 3156 - new async specification
    Tulip - reference implementation of the PEP
    Future?
    Sunday, October 13, 13

    View Slide

  44. Just tell me what should I pip install to make it work...
    So...
    Sunday, October 13, 13

    View Slide

  45. If you really need *high* concurrency, Python probably
    isn’t the tool for the job.

    Sunday, October 13, 13

    View Slide

  46. Thank you!
    Questions?
    Sunday, October 13, 13

    View Slide