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.

2357408ed1ee90461022f24f29a38cbd?s=128

Naren

May 30, 2018
Tweet

Transcript

  1. CONCURRENCY VS PARALLELISM Narendran @DudeWhoCode

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


    
 Cycling | Travelling | Blogging @DudeWhoCode
  3. @DudeWhoCode
 blog.dudewho.codes

  4. SYNC VS ASYNC @DudeWhoCode

  5. CONCURRENT VS PARALLEL @DudeWhoCode

  6. CONCURRENT VS PARALLEL @DudeWhoCode

  7. RECAP • Sync: Blocking operations. • Async: Non blocking operations.

    • Concurrency: Making progress together. • Parallelism: Making progress in parallel. @DudeWhoCode
  8. CONCURRENCY IS NOT PARALLELISM @DudeWhoCode

  9. CONCURRENCY IS NOT PARALLELISM @DudeWhoCode

  10. @DudeWhoCode

  11. @DudeWhoCode

  12. @DudeWhoCode

  13. @DudeWhoCode

  14. @DudeWhoCode

  15. @DudeWhoCode

  16. @DudeWhoCode

  17. @DudeWhoCode

  18. CONCURRENCY IN PYTHON 3 @DudeWhoCode

  19. ASYNCIO • Python 3 standard library to write single-threaded concurrent

    code • Introduced in python 3.4 • Inspired from “twisted” library @DudeWhoCode
  20. ASYNCIO • Event Loops • Coroutines • Futures @DudeWhoCode

  21. EVENT LOOP • Manages and distributes the execution of different

    tasks. •Responsible for registering the tasks and distributing flow of control between them @DudeWhoCode
  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
  23. FUTURES •Objects that represent the result of a task. •Task

    can be completed or pending. •Object may be exceptions too. @DudeWhoCode
  24. FLOW OF EXECUTION @DudeWhoCode

  25. @DudeWhoCode

  26. DIVING DEEP INTO CONCURRENCY… @DudeWhoCode

  27. CONTEXT SWITCH @DudeWhoCode

  28. @DudeWhoCode

  29. BLOCKING TASKS @DudeWhoCode

  30. @DudeWhoCode

  31. ORDER OF EXECUTION @DudeWhoCode

  32. @DudeWhoCode

  33. SYNC VS ASYNC HTTP CALLS @DudeWhoCode

  34. @DudeWhoCode

  35. @DudeWhoCode

  36. RETURN WHEN @DudeWhoCode

  37. @DudeWhoCode

  38. CANCEL FUTURE @DudeWhoCode

  39. @DudeWhoCode

  40. WAIT TIMEOUT @DudeWhoCode

  41. @DudeWhoCode

  42. CONCURRENCY VS PARALLELISM @DudeWhoCode

  43. PARALLEL HTTP CALL @DudeWhoCode

  44. @DudeWhoCode

  45. CONCURRENT HTTP CALL @DudeWhoCode

  46. @DudeWhoCode

  47. CONCURRENCY VS PARALLELISM
 BENCHMARK VIDEO @DudeWhoCode

  48. @DudeWhoCode

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

  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
  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
  52. SUMMARY • Python 3, asyncio, aiohttp, aiofiles • Eventloops, co-routines,

    futures @DudeWhoCode
  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
  54. REFERENCES • AsyncIO for the Working Python Developer (Hackernoon)
 -

    Yeray Diaz • A guide to asynchronous programming in Python with asyncio (FreeCodeCamp)
 - Roman Gaponov @DudeWhoCode
  55. THANK YOU, NAREN @DudeWhoCode Slides:
 www.dudewho.codes/talks @DudeWhoCode