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. $ WHO AM I Naren Senior Software Consultant Tarka Labs


    
 Cycling | Travelling | Blogging @DudeWhoCode
  2. RECAP • Sync: Blocking operations. • Async: Non blocking operations.

    • Concurrency: Making progress together. • Parallelism: Making progress in parallel. @DudeWhoCode
  3. ASYNCIO • Python 3 standard library to write single-threaded concurrent

    code • Introduced in python 3.4 • Inspired from “twisted” library @DudeWhoCode
  4. EVENT LOOP • Manages and distributes the execution of different

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

    can be completed or pending. •Object may be exceptions too. @DudeWhoCode
  7. 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
  8. 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
  9. 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
  10. REFERENCES • AsyncIO for the Working Python Developer (Hackernoon)
 -

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