Slide 1

Slide 1 text

CONCURRENCY VS PARALLELISM Narendran @DudeWhoCode

Slide 2

Slide 2 text

$ WHO AM I Naren Senior Software Consultant Tarka Labs
 
 Cycling | Travelling | Blogging @DudeWhoCode

Slide 3

Slide 3 text

@DudeWhoCode
 blog.dudewho.codes

Slide 4

Slide 4 text

SYNC VS ASYNC @DudeWhoCode

Slide 5

Slide 5 text

CONCURRENT VS PARALLEL @DudeWhoCode

Slide 6

Slide 6 text

CONCURRENT VS PARALLEL @DudeWhoCode

Slide 7

Slide 7 text

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

Slide 8

Slide 8 text

CONCURRENCY IS NOT PARALLELISM @DudeWhoCode

Slide 9

Slide 9 text

CONCURRENCY IS NOT PARALLELISM @DudeWhoCode

Slide 10

Slide 10 text

@DudeWhoCode

Slide 11

Slide 11 text

@DudeWhoCode

Slide 12

Slide 12 text

@DudeWhoCode

Slide 13

Slide 13 text

@DudeWhoCode

Slide 14

Slide 14 text

@DudeWhoCode

Slide 15

Slide 15 text

@DudeWhoCode

Slide 16

Slide 16 text

@DudeWhoCode

Slide 17

Slide 17 text

@DudeWhoCode

Slide 18

Slide 18 text

CONCURRENCY IN PYTHON 3 @DudeWhoCode

Slide 19

Slide 19 text

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

Slide 20

Slide 20 text

ASYNCIO • Event Loops • Coroutines • Futures @DudeWhoCode

Slide 21

Slide 21 text

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

Slide 22

Slide 22 text

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

Slide 23

Slide 23 text

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

Slide 24

Slide 24 text

FLOW OF EXECUTION @DudeWhoCode

Slide 25

Slide 25 text

@DudeWhoCode

Slide 26

Slide 26 text

DIVING DEEP INTO CONCURRENCY… @DudeWhoCode

Slide 27

Slide 27 text

CONTEXT SWITCH @DudeWhoCode

Slide 28

Slide 28 text

@DudeWhoCode

Slide 29

Slide 29 text

BLOCKING TASKS @DudeWhoCode

Slide 30

Slide 30 text

@DudeWhoCode

Slide 31

Slide 31 text

ORDER OF EXECUTION @DudeWhoCode

Slide 32

Slide 32 text

@DudeWhoCode

Slide 33

Slide 33 text

SYNC VS ASYNC HTTP CALLS @DudeWhoCode

Slide 34

Slide 34 text

@DudeWhoCode

Slide 35

Slide 35 text

@DudeWhoCode

Slide 36

Slide 36 text

RETURN WHEN @DudeWhoCode

Slide 37

Slide 37 text

@DudeWhoCode

Slide 38

Slide 38 text

CANCEL FUTURE @DudeWhoCode

Slide 39

Slide 39 text

@DudeWhoCode

Slide 40

Slide 40 text

WAIT TIMEOUT @DudeWhoCode

Slide 41

Slide 41 text

@DudeWhoCode

Slide 42

Slide 42 text

CONCURRENCY VS PARALLELISM @DudeWhoCode

Slide 43

Slide 43 text

PARALLEL HTTP CALL @DudeWhoCode

Slide 44

Slide 44 text

@DudeWhoCode

Slide 45

Slide 45 text

CONCURRENT HTTP CALL @DudeWhoCode

Slide 46

Slide 46 text

@DudeWhoCode

Slide 47

Slide 47 text

CONCURRENCY VS PARALLELISM
 BENCHMARK VIDEO @DudeWhoCode

Slide 48

Slide 48 text

@DudeWhoCode

Slide 49

Slide 49 text

HOW CONCURRENCY IS DIFFERENT FROM MULTI-THREADING? @DudeWhoCode

Slide 50

Slide 50 text

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

Slide 51

Slide 51 text

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

Slide 52

Slide 52 text

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

Slide 53

Slide 53 text

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

Slide 54

Slide 54 text

REFERENCES • AsyncIO for the Working Python Developer (Hackernoon)
 - Yeray Diaz • A guide to asynchronous programming in Python with asyncio (FreeCodeCamp)
 - Roman Gaponov @DudeWhoCode

Slide 55

Slide 55 text

THANK YOU, NAREN @DudeWhoCode Slides:
 www.dudewho.codes/talks @DudeWhoCode