SlideShare a Scribd company logo
1 of 21
Understanding Concurrency
- Anshul Sharma
Agenda
● About the Author
● What is concurrency?
● Threading vs AsyncIO
● When is concurrency useful?
● How to speed up an I/O bound program?
● Synchronous version
● threading version
● asyncio version
● multiprocessing version
● How to speed up a CPU bound program?
● Synchronous version
● threading version
● asyncio version
● multiprocessing version
● Conclusion
What you are about to learn today?
● What is concurrency?
● What are the different features available in python that support concurrency?
● When to use which feature to use to speed up execution?
What you are not going to learn today?
● Detailed programming features available in python to support concurrency.
About the Author
● Product Engineer at Udaan
● Career Coach at Scaler Academy
● Previously worked at Springboard, Amazon & Jabong
● Technology stack familiar with:
● Python/Django
● Java
● Currently learning Kotlin and cooperative multitasking
● Social media handles:
● Linkedin(https://www.linkedin.com/in/raunify/)
● Github(https://github.com/raun)
What is concurrency?
● Dictionary definition of concurrency is simultaneous occurrence.
● In Python, the things that are occurring simultaneously are called by different names (thread, task,
process)
● Each one can be stopped at certain points, and the CPU or brain that is processing them can switch to a
different one.
● In essence only multiprocessing actually does work simultaneously as it uses multiple processors
available on the machine.
● Threading and AsyncIO both run on a single processor. They just cleverly find ways to take turns to
speed up the overall process.
Wondering how Threading and AsyncIO different then?
Threading vs AsyncIO
● Threading
○ In threading, the operating system actually knows about each thread and can interrupt it at any
time to start running a different thread. This is called Pre-emptive multitasking.
○ Pre-emptive multitasking is handy in the sense that code in the thread doesn’t need to do
anything to make the switch. It can also be difficult because of that “at any time” phrase.
● AsyncIO
○ Tasks must cooperate by announcing when they are ready to be switched out. This is called
Cooperative multitasking.
○ That means that the code in the AsyncIO task has to change slightly to announce that it is ready
to be switched out.
○ The benefit of doing this extra work up front is that you always know where your task will be
swapped out.
○ This can simplify parts of your software design.
When is concurrency useful?
● Concurrency can make a big difference for two types of problems.
○ CPU-bound
■ There are classes of programs that do significant computation without talking to the
network or accessing a file.
■ These are the CPU-bound programs, because the resource limiting the speed of your
program is the CPU, not the network or the file system.
○ I/O-bound
■ I/O-bound problems cause your program to slow down because it frequently must wait for
input/output (I/O) from some external resource.
■ They arise frequently when your program is working with things that are much slower than
your CPU.
How to speed up an I/O bound program?
Sample Problem:
Downloading content over the network. For our example, we will be downloading web pages from a few sites,
but it really could be any network traffic.
Synchronous Version
threading Version
asyncio basics
● A single Python object, called the event loop, controls how and when each task gets run.
● The event loop is aware of each task and knows what state it’s in.
● Let's assume a simplified event loop which has 2 states(i.e. Ready to work & Waiting for external
thing)*Python Event Loop can have more state than listed above
● Your simplified event loop maintains two lists of tasks, one for each of these states.
● Your simplified event loop selects one of the ready tasks and starts it back to running. That task is in
complete control until it cooperatively hands the control back to the event loop.
● When the running task gives control back to the event loop, the event loop places that task into
either the ready or waiting list and then goes through each of the tasks in the waiting list to see if it
has become ready by an I/O operation completing.
● An important point of asyncio is that the tasks never give up control without intentionally doing so.
They never get interrupted in the middle of an operation. This allows us to share resources a bit
more easily in asyncio than in threading.
async & await keywords
● async :
○ It is flag to Python telling it that the function about to be defined uses await.*Not always strictly true
○ async with statement, which creates a context manager from an object you would normally
await. But general idea is to flag this context manager as something that can get swapped out.
● await : When your code awaits a function call, it’s a signal that the call is likely to be something
that takes a while and that the task should give up control.
As I’m sure you can imagine, there’s some complexity in managing the interaction between the event loop
and the tasks. But this will get clear with our next example.
asyncio Version
multiprocessing version
How to speed up an CPU bound program?
Sample Problem:
We will use a somewhat silly function to create something that takes a long time to run on the CPU. This
function computes the sum of the squares of each number from 0 to the passed-in value(n)
Synchronous Version
threading & asyncio versions
multiprocessing Version
Conclusion
We have covered a lot of ground here. It’s time to review those ideas and discuss about decision points.
● The first step of this process is deciding if you should use a concurrency module. While the examples here make
each of the libraries look pretty simple, concurrency always comes with extra complexity and can often result in bugs
that are difficult to find.
● Hold out on adding concurrency until you have a known performance issue and then determine which type of
concurrency you need.
● Once you’ve decided that you should optimize your program, figuring out if your program is CPU-bound or I/O-bound.
● CPU-bound problems only really gain from using multiprocessing. threading and asyncio did not help this
type of problem at all.
● For I/O-bound problems, there’s a general rule of thumb in the Python community: “Use asyncio when you can,
threading when you must.”
● asyncio can provide the best speed up for this type of program, but sometimes you will require critical libraries that
have not been ported to take advantage of asyncio.
Q&A
References
● Demo code: https://github.com/raun/concurrency-demo
● Presentation link:

More Related Content

What's hot

Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in javaArafat Hossan
 
Java Multi Thead Programming
Java Multi Thead ProgrammingJava Multi Thead Programming
Java Multi Thead ProgrammingNishant Mevawala
 
Async Web Frameworks in Python
Async Web Frameworks in PythonAsync Web Frameworks in Python
Async Web Frameworks in PythonRyan Johnson
 
The working architecture of NodeJs applications
The working architecture of NodeJs applicationsThe working architecture of NodeJs applications
The working architecture of NodeJs applicationsViktor Turskyi
 
Hibernate concurrency
Hibernate concurrencyHibernate concurrency
Hibernate concurrencypriyank09
 
TestWorks Conf Performance testing made easy with gatling - Guillaume Corré
TestWorks Conf Performance testing made easy with gatling - Guillaume CorréTestWorks Conf Performance testing made easy with gatling - Guillaume Corré
TestWorks Conf Performance testing made easy with gatling - Guillaume CorréXebia Nederland BV
 
Inter - thread communication
Inter - thread communicationInter - thread communication
Inter - thread communicationBhumikaDhingra3
 
Testing in Scala. Adform Research
Testing in Scala. Adform ResearchTesting in Scala. Adform Research
Testing in Scala. Adform ResearchVasil Remeniuk
 
Concurrency - Why it's hard ?
Concurrency - Why it's hard ?Concurrency - Why it's hard ?
Concurrency - Why it's hard ?Ramith Jayasinghe
 
Introduction to Monix Coeval
Introduction to Monix CoevalIntroduction to Monix Coeval
Introduction to Monix CoevalKnoldus Inc.
 
MERIMeeting du 27 mai 2014 - Parallel Programming
MERIMeeting du 27 mai 2014 - Parallel ProgrammingMERIMeeting du 27 mai 2014 - Parallel Programming
MERIMeeting du 27 mai 2014 - Parallel ProgrammingOlivier NAVARRE
 
Let it crash! The Erlang Approach to Building Reliable Services
Let it crash! The Erlang Approach to Building Reliable ServicesLet it crash! The Erlang Approach to Building Reliable Services
Let it crash! The Erlang Approach to Building Reliable ServicesBrian Troutwine
 
Java threads
Java threadsJava threads
Java threadsjavaicon
 

What's hot (20)

RxSwift
RxSwiftRxSwift
RxSwift
 
JAVA Threads explained
JAVA Threads explained JAVA Threads explained
JAVA Threads explained
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
 
Concurrency in c#
Concurrency in c#Concurrency in c#
Concurrency in c#
 
Threading in C#
Threading in C#Threading in C#
Threading in C#
 
Why Concurrency is hard ?
Why Concurrency is hard ?Why Concurrency is hard ?
Why Concurrency is hard ?
 
Concurrency in Java
Concurrency in JavaConcurrency in Java
Concurrency in Java
 
Java Multi Thead Programming
Java Multi Thead ProgrammingJava Multi Thead Programming
Java Multi Thead Programming
 
Async Web Frameworks in Python
Async Web Frameworks in PythonAsync Web Frameworks in Python
Async Web Frameworks in Python
 
The working architecture of NodeJs applications
The working architecture of NodeJs applicationsThe working architecture of NodeJs applications
The working architecture of NodeJs applications
 
Hibernate concurrency
Hibernate concurrencyHibernate concurrency
Hibernate concurrency
 
TestWorks Conf Performance testing made easy with gatling - Guillaume Corré
TestWorks Conf Performance testing made easy with gatling - Guillaume CorréTestWorks Conf Performance testing made easy with gatling - Guillaume Corré
TestWorks Conf Performance testing made easy with gatling - Guillaume Corré
 
Inter - thread communication
Inter - thread communicationInter - thread communication
Inter - thread communication
 
Testing in Scala. Adform Research
Testing in Scala. Adform ResearchTesting in Scala. Adform Research
Testing in Scala. Adform Research
 
Concurrency - Why it's hard ?
Concurrency - Why it's hard ?Concurrency - Why it's hard ?
Concurrency - Why it's hard ?
 
Introduction to Monix Coeval
Introduction to Monix CoevalIntroduction to Monix Coeval
Introduction to Monix Coeval
 
.Net Threading
.Net Threading.Net Threading
.Net Threading
 
MERIMeeting du 27 mai 2014 - Parallel Programming
MERIMeeting du 27 mai 2014 - Parallel ProgrammingMERIMeeting du 27 mai 2014 - Parallel Programming
MERIMeeting du 27 mai 2014 - Parallel Programming
 
Let it crash! The Erlang Approach to Building Reliable Services
Let it crash! The Erlang Approach to Building Reliable ServicesLet it crash! The Erlang Approach to Building Reliable Services
Let it crash! The Erlang Approach to Building Reliable Services
 
Java threads
Java threadsJava threads
Java threads
 

Similar to Understanding concurrency

Exploring the Real Power of Functional Programming
Exploring the Real Power of Functional ProgrammingExploring the Real Power of Functional Programming
Exploring the Real Power of Functional ProgrammingKnoldus Inc.
 
AsyncIO in Python (Guide and Example).pdf
AsyncIO in Python (Guide and Example).pdfAsyncIO in Python (Guide and Example).pdf
AsyncIO in Python (Guide and Example).pdfPreetAujla6
 
Rundown of Async/Await in Rust
Rundown of Async/Await in RustRundown of Async/Await in Rust
Rundown of Async/Await in RustKnoldus Inc.
 
JUST EAT: Embracing DevOps
JUST EAT: Embracing DevOpsJUST EAT: Embracing DevOps
JUST EAT: Embracing DevOpsPeter Mounce
 
icebreakerwithdevops-150218112943-conversion-gate02
icebreakerwithdevops-150218112943-conversion-gate02icebreakerwithdevops-150218112943-conversion-gate02
icebreakerwithdevops-150218112943-conversion-gate02Manohar Kumar
 
Monitoring and automation
Monitoring and automationMonitoring and automation
Monitoring and automationRicardo Bánffy
 
Improve the deployment process step by step
Improve the deployment process step by stepImprove the deployment process step by step
Improve the deployment process step by stepDaniel Fahlke
 
Ice breaker with dev ops
Ice breaker with dev opsIce breaker with dev ops
Ice breaker with dev opsMukta Aphale
 
Serverless - DevOps Lessons Learned From Production
Serverless - DevOps Lessons Learned From ProductionServerless - DevOps Lessons Learned From Production
Serverless - DevOps Lessons Learned From ProductionSteve Hogg
 
Cloud Native CI/CD with Spring Cloud Pipelines
Cloud Native CI/CD with Spring Cloud PipelinesCloud Native CI/CD with Spring Cloud Pipelines
Cloud Native CI/CD with Spring Cloud PipelinesLars Rosenquist
 
Cloud Native CI/CD with Spring Cloud Pipelines
Cloud Native CI/CD with Spring Cloud PipelinesCloud Native CI/CD with Spring Cloud Pipelines
Cloud Native CI/CD with Spring Cloud PipelinesLars Rosenquist
 
Workflows via Event driven architecture
Workflows via Event driven architectureWorkflows via Event driven architecture
Workflows via Event driven architectureMilan Patel
 
Services, tools & practices for a software house
Services, tools & practices for a software houseServices, tools & practices for a software house
Services, tools & practices for a software houseParis Apostolopoulos
 
Continuous Delivery: 5 years later (Incontro DevOps 2018)
Continuous Delivery: 5 years later (Incontro DevOps 2018)Continuous Delivery: 5 years later (Incontro DevOps 2018)
Continuous Delivery: 5 years later (Incontro DevOps 2018)Giovanni Toraldo
 
Getting big without getting fat, in perl
Getting big without getting fat, in perlGetting big without getting fat, in perl
Getting big without getting fat, in perlDean Hamstead
 
RandomTest - Random Software Integration Tests That Just Work for C/C++, Java...
RandomTest - Random Software Integration Tests That Just Work for C/C++, Java...RandomTest - Random Software Integration Tests That Just Work for C/C++, Java...
RandomTest - Random Software Integration Tests That Just Work for C/C++, Java...dcieslak
 
Raising ux bar with offline first design
Raising ux bar with offline first designRaising ux bar with offline first design
Raising ux bar with offline first designKyrylo Reznykov
 
Designing and coding for cloud-native applications using Python, Harjinder Mi...
Designing and coding for cloud-native applications using Python, Harjinder Mi...Designing and coding for cloud-native applications using Python, Harjinder Mi...
Designing and coding for cloud-native applications using Python, Harjinder Mi...Pôle Systematic Paris-Region
 

Similar to Understanding concurrency (20)

Exploring the Real Power of Functional Programming
Exploring the Real Power of Functional ProgrammingExploring the Real Power of Functional Programming
Exploring the Real Power of Functional Programming
 
AsyncIO in Python (Guide and Example).pdf
AsyncIO in Python (Guide and Example).pdfAsyncIO in Python (Guide and Example).pdf
AsyncIO in Python (Guide and Example).pdf
 
Rundown of Async/Await in Rust
Rundown of Async/Await in RustRundown of Async/Await in Rust
Rundown of Async/Await in Rust
 
JUST EAT: Embracing DevOps
JUST EAT: Embracing DevOpsJUST EAT: Embracing DevOps
JUST EAT: Embracing DevOps
 
icebreakerwithdevops-150218112943-conversion-gate02
icebreakerwithdevops-150218112943-conversion-gate02icebreakerwithdevops-150218112943-conversion-gate02
icebreakerwithdevops-150218112943-conversion-gate02
 
Monitoring and automation
Monitoring and automationMonitoring and automation
Monitoring and automation
 
Improve the deployment process step by step
Improve the deployment process step by stepImprove the deployment process step by step
Improve the deployment process step by step
 
Ice breaker with dev ops
Ice breaker with dev opsIce breaker with dev ops
Ice breaker with dev ops
 
Serverless - DevOps Lessons Learned From Production
Serverless - DevOps Lessons Learned From ProductionServerless - DevOps Lessons Learned From Production
Serverless - DevOps Lessons Learned From Production
 
Cloud Native CI/CD with Spring Cloud Pipelines
Cloud Native CI/CD with Spring Cloud PipelinesCloud Native CI/CD with Spring Cloud Pipelines
Cloud Native CI/CD with Spring Cloud Pipelines
 
Cloud Native CI/CD with Spring Cloud Pipelines
Cloud Native CI/CD with Spring Cloud PipelinesCloud Native CI/CD with Spring Cloud Pipelines
Cloud Native CI/CD with Spring Cloud Pipelines
 
Workflows via Event driven architecture
Workflows via Event driven architectureWorkflows via Event driven architecture
Workflows via Event driven architecture
 
Training – Going Async
Training – Going AsyncTraining – Going Async
Training – Going Async
 
Services, tools & practices for a software house
Services, tools & practices for a software houseServices, tools & practices for a software house
Services, tools & practices for a software house
 
Continuous Delivery: 5 years later (Incontro DevOps 2018)
Continuous Delivery: 5 years later (Incontro DevOps 2018)Continuous Delivery: 5 years later (Incontro DevOps 2018)
Continuous Delivery: 5 years later (Incontro DevOps 2018)
 
Getting big without getting fat, in perl
Getting big without getting fat, in perlGetting big without getting fat, in perl
Getting big without getting fat, in perl
 
Automation tools: making things go... (March 2019)
Automation tools: making things go... (March 2019)Automation tools: making things go... (March 2019)
Automation tools: making things go... (March 2019)
 
RandomTest - Random Software Integration Tests That Just Work for C/C++, Java...
RandomTest - Random Software Integration Tests That Just Work for C/C++, Java...RandomTest - Random Software Integration Tests That Just Work for C/C++, Java...
RandomTest - Random Software Integration Tests That Just Work for C/C++, Java...
 
Raising ux bar with offline first design
Raising ux bar with offline first designRaising ux bar with offline first design
Raising ux bar with offline first design
 
Designing and coding for cloud-native applications using Python, Harjinder Mi...
Designing and coding for cloud-native applications using Python, Harjinder Mi...Designing and coding for cloud-native applications using Python, Harjinder Mi...
Designing and coding for cloud-native applications using Python, Harjinder Mi...
 

More from Anshul Sharma (12)

Interm codegen
Interm codegenInterm codegen
Interm codegen
 
Programming using Open Mp
Programming using Open MpProgramming using Open Mp
Programming using Open Mp
 
Open MPI 2
Open MPI 2Open MPI 2
Open MPI 2
 
Open MPI
Open MPIOpen MPI
Open MPI
 
Paralle programming 2
Paralle programming 2Paralle programming 2
Paralle programming 2
 
Parallel programming
Parallel programmingParallel programming
Parallel programming
 
Cuda 3
Cuda 3Cuda 3
Cuda 3
 
Cuda 2
Cuda 2Cuda 2
Cuda 2
 
Cuda intro
Cuda introCuda intro
Cuda intro
 
Des
DesDes
Des
 
Intoduction to Linux
Intoduction to LinuxIntoduction to Linux
Intoduction to Linux
 
GCC
GCCGCC
GCC
 

Recently uploaded

The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...kalichargn70th171
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsJhone kinadey
 
The Top App Development Trends Shaping the Industry in 2024-25 .pdf
The Top App Development Trends Shaping the Industry in 2024-25 .pdfThe Top App Development Trends Shaping the Industry in 2024-25 .pdf
The Top App Development Trends Shaping the Industry in 2024-25 .pdfayushiqss
 
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdfPayment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdfkalichargn70th171
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...Health
 
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park masabamasaba
 
Chinsurah Escorts ☎️8617697112 Starting From 5K to 15K High Profile Escorts ...
Chinsurah Escorts ☎️8617697112  Starting From 5K to 15K High Profile Escorts ...Chinsurah Escorts ☎️8617697112  Starting From 5K to 15K High Profile Escorts ...
Chinsurah Escorts ☎️8617697112 Starting From 5K to 15K High Profile Escorts ...Nitya salvi
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️Delhi Call girls
 
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfonteinmasabamasaba
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providermohitmore19
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnAmarnathKambale
 
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park %in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park masabamasaba
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfkalichargn70th171
 
Pharm-D Biostatistics and Research methodology
Pharm-D Biostatistics and Research methodologyPharm-D Biostatistics and Research methodology
Pharm-D Biostatistics and Research methodologyAnusha Are
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Modelsaagamshah0812
 
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfonteinmasabamasaba
 
Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdf
Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdfAzure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdf
Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdfryanfarris8
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVshikhaohhpro
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerThousandEyes
 
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension AidPhilip Schwarz
 

Recently uploaded (20)

The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 
The Top App Development Trends Shaping the Industry in 2024-25 .pdf
The Top App Development Trends Shaping the Industry in 2024-25 .pdfThe Top App Development Trends Shaping the Industry in 2024-25 .pdf
The Top App Development Trends Shaping the Industry in 2024-25 .pdf
 
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdfPayment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
 
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
 
Chinsurah Escorts ☎️8617697112 Starting From 5K to 15K High Profile Escorts ...
Chinsurah Escorts ☎️8617697112  Starting From 5K to 15K High Profile Escorts ...Chinsurah Escorts ☎️8617697112  Starting From 5K to 15K High Profile Escorts ...
Chinsurah Escorts ☎️8617697112 Starting From 5K to 15K High Profile Escorts ...
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learn
 
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park %in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
 
Pharm-D Biostatistics and Research methodology
Pharm-D Biostatistics and Research methodologyPharm-D Biostatistics and Research methodology
Pharm-D Biostatistics and Research methodology
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Models
 
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
 
Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdf
Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdfAzure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdf
Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdf
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTV
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
 
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
 

Understanding concurrency

  • 2. Agenda ● About the Author ● What is concurrency? ● Threading vs AsyncIO ● When is concurrency useful? ● How to speed up an I/O bound program? ● Synchronous version ● threading version ● asyncio version ● multiprocessing version ● How to speed up a CPU bound program? ● Synchronous version ● threading version ● asyncio version ● multiprocessing version ● Conclusion
  • 3. What you are about to learn today? ● What is concurrency? ● What are the different features available in python that support concurrency? ● When to use which feature to use to speed up execution? What you are not going to learn today? ● Detailed programming features available in python to support concurrency.
  • 4. About the Author ● Product Engineer at Udaan ● Career Coach at Scaler Academy ● Previously worked at Springboard, Amazon & Jabong ● Technology stack familiar with: ● Python/Django ● Java ● Currently learning Kotlin and cooperative multitasking ● Social media handles: ● Linkedin(https://www.linkedin.com/in/raunify/) ● Github(https://github.com/raun)
  • 5. What is concurrency? ● Dictionary definition of concurrency is simultaneous occurrence. ● In Python, the things that are occurring simultaneously are called by different names (thread, task, process) ● Each one can be stopped at certain points, and the CPU or brain that is processing them can switch to a different one. ● In essence only multiprocessing actually does work simultaneously as it uses multiple processors available on the machine. ● Threading and AsyncIO both run on a single processor. They just cleverly find ways to take turns to speed up the overall process. Wondering how Threading and AsyncIO different then?
  • 6. Threading vs AsyncIO ● Threading ○ In threading, the operating system actually knows about each thread and can interrupt it at any time to start running a different thread. This is called Pre-emptive multitasking. ○ Pre-emptive multitasking is handy in the sense that code in the thread doesn’t need to do anything to make the switch. It can also be difficult because of that “at any time” phrase. ● AsyncIO ○ Tasks must cooperate by announcing when they are ready to be switched out. This is called Cooperative multitasking. ○ That means that the code in the AsyncIO task has to change slightly to announce that it is ready to be switched out. ○ The benefit of doing this extra work up front is that you always know where your task will be swapped out. ○ This can simplify parts of your software design.
  • 7. When is concurrency useful? ● Concurrency can make a big difference for two types of problems. ○ CPU-bound ■ There are classes of programs that do significant computation without talking to the network or accessing a file. ■ These are the CPU-bound programs, because the resource limiting the speed of your program is the CPU, not the network or the file system. ○ I/O-bound ■ I/O-bound problems cause your program to slow down because it frequently must wait for input/output (I/O) from some external resource. ■ They arise frequently when your program is working with things that are much slower than your CPU.
  • 8. How to speed up an I/O bound program? Sample Problem: Downloading content over the network. For our example, we will be downloading web pages from a few sites, but it really could be any network traffic.
  • 11. asyncio basics ● A single Python object, called the event loop, controls how and when each task gets run. ● The event loop is aware of each task and knows what state it’s in. ● Let's assume a simplified event loop which has 2 states(i.e. Ready to work & Waiting for external thing)*Python Event Loop can have more state than listed above ● Your simplified event loop maintains two lists of tasks, one for each of these states. ● Your simplified event loop selects one of the ready tasks and starts it back to running. That task is in complete control until it cooperatively hands the control back to the event loop. ● When the running task gives control back to the event loop, the event loop places that task into either the ready or waiting list and then goes through each of the tasks in the waiting list to see if it has become ready by an I/O operation completing. ● An important point of asyncio is that the tasks never give up control without intentionally doing so. They never get interrupted in the middle of an operation. This allows us to share resources a bit more easily in asyncio than in threading.
  • 12. async & await keywords ● async : ○ It is flag to Python telling it that the function about to be defined uses await.*Not always strictly true ○ async with statement, which creates a context manager from an object you would normally await. But general idea is to flag this context manager as something that can get swapped out. ● await : When your code awaits a function call, it’s a signal that the call is likely to be something that takes a while and that the task should give up control. As I’m sure you can imagine, there’s some complexity in managing the interaction between the event loop and the tasks. But this will get clear with our next example.
  • 15. How to speed up an CPU bound program? Sample Problem: We will use a somewhat silly function to create something that takes a long time to run on the CPU. This function computes the sum of the squares of each number from 0 to the passed-in value(n)
  • 19. Conclusion We have covered a lot of ground here. It’s time to review those ideas and discuss about decision points. ● The first step of this process is deciding if you should use a concurrency module. While the examples here make each of the libraries look pretty simple, concurrency always comes with extra complexity and can often result in bugs that are difficult to find. ● Hold out on adding concurrency until you have a known performance issue and then determine which type of concurrency you need. ● Once you’ve decided that you should optimize your program, figuring out if your program is CPU-bound or I/O-bound. ● CPU-bound problems only really gain from using multiprocessing. threading and asyncio did not help this type of problem at all. ● For I/O-bound problems, there’s a general rule of thumb in the Python community: “Use asyncio when you can, threading when you must.” ● asyncio can provide the best speed up for this type of program, but sometimes you will require critical libraries that have not been ported to take advantage of asyncio.
  • 20. Q&A
  • 21. References ● Demo code: https://github.com/raun/concurrency-demo ● Presentation link:

Editor's Notes

  1. Why the synchronous version rocks? It is easy! Easy to debug. One train of thought Problems with synchronous version Its dead slow! Although being slower is not necessarily a big issue If it runs rarely If the completion time is within SLA What if your program run frequently(Those daily tasks)? What if it takes hours to run? Then we have to think about something better!
  2. download_all_sites() changed from calling the function once per site to a more complex structure In this version, you’re creating a ThreadPoolExecutor, which seems like a complicated thing. Let’s break that down: ThreadPoolExecutor = Thread + Pool + Executor. This object is going to create a pool of threads, each of which can run concurrently. The Executor is the part that’s going to control how and when each of the threads in the pool will run. map() method runs the passed-in function on each of the sites in the list. The great part is that it automatically runs them concurrently using the pool of threads it is managing. The other interesting change in our example is that each thread needs to create its own requests.Session() object. Because the operating system is in control of when your task gets interrupted and another task starts, any data that is shared between the threads needs to be protected, or thread-safe. Threading.local() creates an object that look like a global but is specific to each individual thread. How to decide the number of thread in the pool? This require a little hit and trial. You might expect that having one thread per download would be the fastest but, at least on my system it was not. Why is that? There are two main competing force working here: Speed up due to concurrency Overhead of creating & destroying thread On my machine, the optimal value was 10 threads. Another interesting thing is, with 10 threads, the time has reduced slightly more than 1/10th of the original time. Why is that? Why the threading version rocks? Its fast! Or atleast faster than the synchronous version. But why? Allow your program to overlap the waiting times and get the final result faster Problems with threading version Little more code to make it happen and think about how to keep objects and code thread safe. Threads can interact in ways that are subtle and hard to detect. These interaction can cause race conditions which can result random, intermittent bugs that are difficult to debug.
  3. Why the asyncio version rocks? It is faster than the threading version. It forces you to think about when a given task will get swapped out, which can help you create a better, faster, design. It is much more scalable. With threading version you have to fine tune the number of thread based on problem and hardware you are working with. Problems with asyncio version You need special async versions of libraries to gain the full advantage of asyncio. Had you just used requests for downloading the sites, it would have been much slower because requests is not designed to notify the event loop that it’s blocked. All of the advantages of cooperative multitasking get thrown away if one of the tasks doesn’t cooperate. A minor mistake in code can cause a task to run off and hold the processor for a long time, starving other tasks that need running. There is no way for the event loop to break in if a task does not hand control back to it.
  4. Code looks is much shorter than asyncio version multiprocessing on high level create a new instance of Python interpreter to run on each CPU and then farming out part of our program to run on it. Why the multiprogramming version rocks? Relatively easy to setup & requires little extra code Takes full advantage of the CPU power on your computer Problems with asyncio version Slower than asyncio and threading Require some setup from synchronous version Spent some time thinking about which variables will be accessed in each process
  5. This code calls cpu_bound() 20 times with a different large number each time. It does all of this on a single thread in a single process on a single CPU. Unlike the I/O-bound examples, the CPU-bound examples are usually fairly consistent in their run times. Why is that? Clearly we can do better than this. This is all running on a single CPU with no concurrency. How much do you think rewriting this code using threading or asyncio will speed this up?
  6. In your I/O-bound example, much of the overall time was spent waiting for slow operations to finish. threading and asyncio sped this up by allowing you to overlap the times you were waiting instead of doing them sequentially. On a CPU-bound problem, however, there is no waiting. The CPU is cranking away as fast as it can to finish the problem. One CPU is doing all of the work of the non-concurrent code plus the extra work of setting up threads or tasks.
  7. Why the multiprocessing version rocks? Easy to setup with very little code Take full advantage of CPU power on your computer Problems with multiprocessing version Splitting your problem up so each processor can work independently can sometimes be difficult. Many solutions require more communication between the processes. This would add more complexity to your synchronous version and sometime these communication overhead can outweigh the benefit.