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

Concurrency vs Parallelism

Concurrency vs Parallelism

A good code is one which uses the system resources efficiently which means not over utilizing the resources as well as not under utilizing by leaving them idle. One of the main features of Python3 is its asynchronous capabilities. After getting to know what is concurrency and why concurrency is not parallelism. We will see how can we leverage the asyncio library's functionalities to design and run our code concurrently, use our resources efficiently and finish the tasks blazingly fast.

Naren

May 30, 2018
Tweet

More Decks by Naren

Other Decks in Programming

Transcript

  1. CONCURRENCY
    VS
    PARALLELISM
    Narendran
    @DudeWhoCode

    View Slide

  2. $ WHO AM I
    Naren
    Senior Software Consultant
    Tarka Labs


    Cycling | Travelling | Blogging
    @DudeWhoCode

    View Slide

  3. @DudeWhoCode

    blog.dudewho.codes

    View Slide

  4. SYNC VS ASYNC
    @DudeWhoCode

    View Slide

  5. CONCURRENT VS PARALLEL
    @DudeWhoCode

    View Slide

  6. CONCURRENT VS PARALLEL
    @DudeWhoCode

    View Slide

  7. RECAP
    • Sync: Blocking operations.
    • Async: Non blocking operations.
    • Concurrency: Making progress together.
    • Parallelism: Making progress in parallel.
    @DudeWhoCode

    View Slide

  8. CONCURRENCY IS NOT PARALLELISM
    @DudeWhoCode

    View Slide

  9. CONCURRENCY IS NOT PARALLELISM
    @DudeWhoCode

    View Slide

  10. @DudeWhoCode

    View Slide

  11. @DudeWhoCode

    View Slide

  12. @DudeWhoCode

    View Slide

  13. @DudeWhoCode

    View Slide

  14. @DudeWhoCode

    View Slide

  15. @DudeWhoCode

    View Slide

  16. @DudeWhoCode

    View Slide

  17. @DudeWhoCode

    View Slide

  18. CONCURRENCY IN PYTHON
    3
    @DudeWhoCode

    View Slide

  19. ASYNCIO
    • Python 3 standard library to write single-threaded
    concurrent code
    • Introduced in python 3.4
    • Inspired from “twisted” library
    @DudeWhoCode

    View Slide

  20. ASYNCIO
    • Event Loops
    • Coroutines
    • Futures
    @DudeWhoCode

    View Slide

  21. EVENT LOOP
    • Manages and distributes the execution of different
    tasks.
    •Responsible for registering the tasks and distributing
    flow of control between them
    @DudeWhoCode

    View Slide

  22. COROUTINES
    •Special functions capable of releasing the flow of
    control back to the event loop.
    •Similar to python generators
    •A coroutine is scheduled using an event loop
    @DudeWhoCode

    View Slide

  23. FUTURES
    •Objects that represent the result of a task.
    •Task can be completed or pending.
    •Object may be exceptions too.
    @DudeWhoCode

    View Slide

  24. FLOW OF EXECUTION
    @DudeWhoCode

    View Slide

  25. @DudeWhoCode

    View Slide

  26. DIVING DEEP INTO CONCURRENCY…
    @DudeWhoCode

    View Slide

  27. CONTEXT SWITCH
    @DudeWhoCode

    View Slide

  28. @DudeWhoCode

    View Slide

  29. BLOCKING TASKS
    @DudeWhoCode

    View Slide

  30. @DudeWhoCode

    View Slide

  31. ORDER OF EXECUTION
    @DudeWhoCode

    View Slide

  32. @DudeWhoCode

    View Slide

  33. SYNC VS ASYNC HTTP CALLS
    @DudeWhoCode

    View Slide

  34. @DudeWhoCode

    View Slide

  35. @DudeWhoCode

    View Slide

  36. RETURN WHEN
    @DudeWhoCode

    View Slide

  37. @DudeWhoCode

    View Slide

  38. CANCEL FUTURE
    @DudeWhoCode

    View Slide

  39. @DudeWhoCode

    View Slide

  40. WAIT TIMEOUT
    @DudeWhoCode

    View Slide

  41. @DudeWhoCode

    View Slide

  42. CONCURRENCY VS PARALLELISM
    @DudeWhoCode

    View Slide

  43. PARALLEL HTTP CALL
    @DudeWhoCode

    View Slide

  44. @DudeWhoCode

    View Slide

  45. CONCURRENT HTTP CALL
    @DudeWhoCode

    View Slide

  46. @DudeWhoCode

    View Slide

  47. CONCURRENCY VS PARALLELISM

    BENCHMARK VIDEO
    @DudeWhoCode

    View Slide

  48. @DudeWhoCode

    View Slide

  49. HOW CONCURRENCY IS DIFFERENT FROM
    MULTI-THREADING?
    @DudeWhoCode

    View Slide

  50. THREADING VS ASYNC
    • Async : Mostly single threaded, You decide when
    a piece of code can take back control using await
    • Threading : Python scheduler takes care of
    context switch and it may lose control anytime.
    @DudeWhoCode

    View Slide

  51. SUMMARY
    • Sync: Blocking operations.
    • Async: Non blocking operations.
    • Concurrency: Creating subtasks, scheduling subtasks,
    switching between them, making progress together.
    • Parallelism: Creating subtasks, utilize CPU core,
    making progress in parallel.
    @DudeWhoCode

    View Slide

  52. SUMMARY
    • Python 3, asyncio, aiohttp, aiofiles
    • Eventloops, co-routines, futures
    @DudeWhoCode

    View Slide

  53. SUMMARY
    • context switching
    • sync vs async http calls
    • no order of execution
    • return_when
    • cancel pending future
    • wait timeout
    • parallel vs concurrent http calls
    @DudeWhoCode

    View Slide

  54. REFERENCES
    • AsyncIO for the Working Python Developer (Hackernoon)

    - Yeray Diaz
    • A guide to asynchronous programming in Python with asyncio
    (FreeCodeCamp)

    - Roman Gaponov
    @DudeWhoCode

    View Slide

  55. THANK YOU,
    NAREN
    @DudeWhoCode
    Slides:

    www.dudewho.codes/talks
    @DudeWhoCode

    View Slide