Concurrency

using Python

Understanding the basic concepts

of

$whoami: Shashank

Writes software for Nykaa.com (Nykaa Display Network)

Open-source enthusiast, works mostly on Python, JS & related technologies

https://shashankaryan.tech | aryanshashank31@gmail.com

Contact Details

Contents:

  • Concurrency vs Parallelism - thread, task and process
  • How to use Concurrency - I/O Bound vs CPU Bound Programs - Demo
  • Conclusion - When to use Concurrency?

Note: This talk doesn't include any specificity about the libraries used

Concurrency       v/s        Parallelism  

clever switching of tasks on a single core 

performing computations using multiple cores  

Concept of thread, task and process

Thread - threading| Pre-emptive multitasking | Single core of CPU

Task - asyncIO | Cooperative Multitasking | Single core of CPU

Process - multiprocessing - Multiple cores of CPU

How to use concurrency?

Use Case 1: I/O Bound Programs 

- spends most of its time interacting with a I/O endpoint, like a network connection, a hard drive, etc.

- speeding involves overlapping of waiting time, spent for such interaction

DEMO - A program that gets multiple URLs and prints their length.

Synchronous version

threading Version

asyncIO Version

multiprocessing Version

Use Case 2: CPU Bound Programs

- speeding involves more computation with the same time duration

- spends most of its time performing CPU computations.

DEMO - A program that computes the sum of items in a very large list.

Synchronous version

What will be the result if we apply threading or asyncIO versions for the CPU bound process?

Question!

multiprocessing Version

Conclusion: When to use Concurrency?

"Premature optimization is the root of all evil." - Donald Knuth

Questions/Suggestions/Discussion

Thank You :)

Concurrency 101

By Shashank Aryan