SlideShare a Scribd company logo
1 of 26
Download to read offline
…….Asynchronous Python:
A Gentle Introduction………
James Cropcho, PyData New York 2017
Structure of Presentation
(1) Define asynchronous programming, thereby disambiguating related but
independent concepts
(2) Define coroutines
(3) Survey Python asynchronous programming implementations
(4) First steps towards asynchrony for a program
(5) Some asyncio tips
I target CPython 3.7 only, and ignore deprecated interfaces.
I’m no longer comparing implementations using different libraries, as I’d planned in
the talk proposal.
Please do not hold questions until the end.
You don’t need me around to look up the exact syntax for what you’re trying to do
via StackOverflow; I am here to get you to that point, where you are putting down
brass tacks.
I also aim to provide “evergreen” material which does not become irrelevant
rapidly.
Processes and Threads
Shared memory: Threads, yes. Processes, no.
“The principal challenge of multi-threaded applications is coordinating threads that
share data or other resources. To that end, the threading module provides a
number of synchronization primitives including locks, events, condition variables,
and semaphores. …While those tools are powerful, minor design errors can
result in problems that are difficult to reproduce.” (Multi-threading)
Processes: interprocess communication
Concurrent Computing vs. Parallel Computing
“Parallel computing is closely related to concurrent computing—they are frequently
used together, and often conflated, though the two are distinct: it is possible to
have parallelism without concurrency (such as bit-level parallelism), and
concurrency without parallelism (such as multitasking by time-sharing on a
single-core CPU).[5][6]” (Wikipedia: Parallel Computing)
Preemptive vs. Cooperative Multitasking
With cooperative multitasking, the code “volunteers” to give up control.
Blocking vs non-blocking
Blocking: “non-asynchronous”
“Can I do something else while I’m waiting?
So What is Asynchrony?
Generally regarded as a single-threaded programming “style”/”paradigm” with a
queue of non-blocking tasks.
One thing at a time!
Baking bread while doing dishes (state is retained across return to a previous
context)
So, as commonly stated, is there a speed boost from this?
@sixty_north
Coroutine Concurrency in Python 3
Getting to grips with asyncio
Robert Smallshire
@robsmallshire
1
Definitions #1
Dealing with multiple things at once versus doing multiple things at once.
Concurrency Parallelism
Tasks start, run, and
complete in
overlapping time
periods
Tasks run
simultaneously
coroutines
1
threads /
processes
+ multicore
Definitions #2
The definition of synchronous contradicts common usage
Sequential
(Synchronous)
Must complete before
proceeding
Asynchronous
No need to wait
before
proceeding
overall duration
shorter 1
overall duration
longer
Definitions #4
Coöperative
multitasking
Tasks yield to
scheduler
Preëmptive
multitasking
Scheduler
interrupts tasks
Inconvenient context
switches 1
Uncooperative tasks hang
system
Coroutines
“Coroutines are computer-program components that generalize subroutines for
non-preemptive multitasking, by allowing multiple entry points for suspending and
resuming execution at certain locations” (Wikipedia: Coroutines)
Subroutines are a subset of coroutines. Generators (semicoroutines) are between
the two.
Tasklets, generators, async/await vs generator syntax, greenlets…
Coroutine vs coroutine objects (Smallshire): code only, callable vs code+state,
awaitable
Coroutines 2
Things a coroutine can do (18.5 asyncio):
● result = await future or result = yield from future
● result = await coroutine or result = yield from coroutine
● return expression
● raise exception
1
Filter and print coroutine
Similar to async_search, but prints all matches
def async_print_matches(iterable,
for item in iterable:
if predicate(item):
predicate):
print("Found :",
item,
yield
end=",
")
Event Loops
1. while 1 (Maxwell):
2. wait for something to happen
3. react to whatever happened
…and Executors: Executors are great for if one must really use a synch/blocking
library (Langa)
Task: making a coroutine into a future, and enqueueing it upon the event loop
(asyncio-speak only)
I/O aware scheduler
Tasks suspend when waiting, scheduled when data ready
1
2
3
4
5
6
7
8waiting on
I/O
I/O
ready
scheduled
1
Round-robin scheduler
Tasks run in circular order
1
2
3
4
5
6
7
8
1
Why is This Difficult?
“The principal challenge of multi-threaded applications is coordinating threads that
share data or other resources.” (11.4 Multi-threading)
Subtler feedback/failures
Simultaneously concerned with different abstraction/interface/implementation
levels, i.e. concerned with implementation in the same space/code as interface
AND: this is because it’s cooperative multitasking OR: concerned with stuff going
on outside of the thread/context
First steps towards asynchrony for a program
Scaling: “Processes: tens, Threads: hundreds, Async: thousands” (Grinberg)
First ask: is there already an asynchronous package to do this precise thing?
aio-libs is very helpful for this!
Regardless, asyncio is probably your best bet.
Python Asynchronous Implementations
asycio is a massive framework encompassing high and low abstraction APIs.
Much other tooling is built atop it.
Stackless Python: allows “tasklets” which are like coroutines/coroutine-futures
Both Eventlet and Gevent use Greenlet. Greenlet is a spin-off of Stackless.
“Greenlets are provided as a C extension module for the regular unmodified
interpreter.” A “greenlet” is a coroutine. (Greenlet GH)
“gevent is inspired by eventlet” (Gevent site)
Curio and Trio (new kids on the block), but worth knowing of
asyncio Tips
PYTHONASYNCIODEBUG=1 python -Wdefault groovy-threads.py
“Long CPU-intensive tasks must routinely release the CPU to avoid starving other
tasks. This can be done by “sleeping” periodically, such as once per loop iteration.
To tell the loop to return control back as soon as possible, sleep for 0 seconds.
Example: await asyncio.sleep(0)” (Grinberg) NOTE: When is this less
important?
Blocking library functions are incompatible with async frameworks (Grinberg)
socket.*, select.* subprocess.*, os.waitpid threading.*,
multiprocessing.* time.sleep
Further Learning: refer to ‘Works Cited’
Works [Heavily] Cited and This Work’s License
(1) https://commons.wikimedia.org/wiki/File:Open_Make_Up_For_Ever_2013_-_
Team_-_France_-_15.jpg
(2) Robert Smallshire (2017) , Getting To Grips With asyncio. Sixty North
(3) Demystifying async, await, and asyncio by Sebastiaan Mathôt
(4) [18.5. asyncio — Asynchronous I/O, event loop, coroutines and tasks]
https://docs.python.org/3.7/library/asyncio.html
(5) [11.4. Multi-threading]
https://docs.python.org/3.7/tutorial/stdlib2.html#multi-threading
(6) Miguel Grinberg, Asynchronous Python for the Complete Beginner
Distributed under the Creative Commons Attribution-ShareAlike 4.0 International license,
save for Smallshire’s slides.
Works [Heavily] Cited 2
(1) Łukasz Langa - Thinking In Coroutines - PyCon 2016
(2) [Maxwell]
https://www.quora.com/How-does-an-event-loop-work/answer/Timothy-Maxwell
Distributed under the Creative Commons Attribution-ShareAlike 4.0 International license,
save for Smallshire’s slides.
Question and Answer Period
Also…
I am seeking pan-industry Python contractor work
and a job in finance.
Got anything for me?

More Related Content

What's hot

What's hot (20)

How to build a streaming Lakehouse with Flink, Kafka, and Hudi
How to build a streaming Lakehouse with Flink, Kafka, and HudiHow to build a streaming Lakehouse with Flink, Kafka, and Hudi
How to build a streaming Lakehouse with Flink, Kafka, and Hudi
 
Flexible and Real-Time Stream Processing with Apache Flink
Flexible and Real-Time Stream Processing with Apache FlinkFlexible and Real-Time Stream Processing with Apache Flink
Flexible and Real-Time Stream Processing with Apache Flink
 
Edge architecture ieee international conference on cloud engineering
Edge architecture   ieee international conference on cloud engineeringEdge architecture   ieee international conference on cloud engineering
Edge architecture ieee international conference on cloud engineering
 
Event Sourcing & CQRS, Kafka, Rabbit MQ
Event Sourcing & CQRS, Kafka, Rabbit MQEvent Sourcing & CQRS, Kafka, Rabbit MQ
Event Sourcing & CQRS, Kafka, Rabbit MQ
 
Spark (Structured) Streaming vs. Kafka Streams
Spark (Structured) Streaming vs. Kafka StreamsSpark (Structured) Streaming vs. Kafka Streams
Spark (Structured) Streaming vs. Kafka Streams
 
Introduction to Apache Kafka
Introduction to Apache KafkaIntroduction to Apache Kafka
Introduction to Apache Kafka
 
Pinot: Near Realtime Analytics @ Uber
Pinot: Near Realtime Analytics @ UberPinot: Near Realtime Analytics @ Uber
Pinot: Near Realtime Analytics @ Uber
 
MySQL Monitoring using Prometheus & Grafana
MySQL Monitoring using Prometheus & GrafanaMySQL Monitoring using Prometheus & Grafana
MySQL Monitoring using Prometheus & Grafana
 
Apache Airflow Architecture
Apache Airflow ArchitectureApache Airflow Architecture
Apache Airflow Architecture
 
Running Apache Spark on Kubernetes: Best Practices and Pitfalls
Running Apache Spark on Kubernetes: Best Practices and PitfallsRunning Apache Spark on Kubernetes: Best Practices and Pitfalls
Running Apache Spark on Kubernetes: Best Practices and Pitfalls
 
DDD와 이벤트소싱
DDD와 이벤트소싱DDD와 이벤트소싱
DDD와 이벤트소싱
 
Exactly-Once Financial Data Processing at Scale with Flink and Pinot
Exactly-Once Financial Data Processing at Scale with Flink and PinotExactly-Once Financial Data Processing at Scale with Flink and Pinot
Exactly-Once Financial Data Processing at Scale with Flink and Pinot
 
Apache Airflow
Apache AirflowApache Airflow
Apache Airflow
 
Hyperloglog Project
Hyperloglog ProjectHyperloglog Project
Hyperloglog Project
 
Microservices Docker Kubernetes Istio Kanban DevOps SRE
Microservices Docker Kubernetes Istio Kanban DevOps SREMicroservices Docker Kubernetes Istio Kanban DevOps SRE
Microservices Docker Kubernetes Istio Kanban DevOps SRE
 
Airflow - a data flow engine
Airflow - a data flow engineAirflow - a data flow engine
Airflow - a data flow engine
 
Models for hierarchical data
Models for hierarchical dataModels for hierarchical data
Models for hierarchical data
 
Apache Tez – Present and Future
Apache Tez – Present and FutureApache Tez – Present and Future
Apache Tez – Present and Future
 
Gcp dataflow
Gcp dataflowGcp dataflow
Gcp dataflow
 
Apache Airflow
Apache AirflowApache Airflow
Apache Airflow
 

Similar to Asynchronous Python A Gentle Introduction

The Pillars Of Concurrency
The Pillars Of ConcurrencyThe Pillars Of Concurrency
The Pillars Of Concurrency
aviade
 
Asynchronous programming
Asynchronous programmingAsynchronous programming
Asynchronous programming
Chester Hartin
 

Similar to Asynchronous Python A Gentle Introduction (20)

PyCon Canada 2019 - Introduction to Asynchronous Programming
PyCon Canada 2019 - Introduction to Asynchronous ProgrammingPyCon Canada 2019 - Introduction to Asynchronous Programming
PyCon Canada 2019 - Introduction to Asynchronous Programming
 
Let's Talk Locks!
Let's Talk Locks!Let's Talk Locks!
Let's Talk Locks!
 
The art of concurrent programming
The art of concurrent programmingThe art of concurrent programming
The art of concurrent programming
 
concurrency
concurrencyconcurrency
concurrency
 
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
 
Need for Async: Hot pursuit for scalable applications
Need for Async: Hot pursuit for scalable applicationsNeed for Async: Hot pursuit for scalable applications
Need for Async: Hot pursuit for scalable applications
 
The Pillars Of Concurrency
The Pillars Of ConcurrencyThe Pillars Of Concurrency
The Pillars Of Concurrency
 
Intro to Multitasking
Intro to MultitaskingIntro to Multitasking
Intro to Multitasking
 
MultiThreading in Python
MultiThreading in PythonMultiThreading in Python
MultiThreading in Python
 
Async-await best practices in 10 minutes
Async-await best practices in 10 minutesAsync-await best practices in 10 minutes
Async-await best practices in 10 minutes
 
Evolution of JDK Tools for Multithreaded Programming
Evolution of JDK Tools for Multithreaded ProgrammingEvolution of JDK Tools for Multithreaded Programming
Evolution of JDK Tools for Multithreaded Programming
 
.NET Multithreading/Multitasking
.NET Multithreading/Multitasking.NET Multithreading/Multitasking
.NET Multithreading/Multitasking
 
Here comes the Loom - Ya!vaConf.pdf
Here comes the Loom - Ya!vaConf.pdfHere comes the Loom - Ya!vaConf.pdf
Here comes the Loom - Ya!vaConf.pdf
 
Adsa u4 ver 1.0
Adsa u4 ver 1.0Adsa u4 ver 1.0
Adsa u4 ver 1.0
 
Task parallel library presentation
Task parallel library presentationTask parallel library presentation
Task parallel library presentation
 
Copy of Multithreaded Programming.pptx
Copy of Multithreaded Programming.pptxCopy of Multithreaded Programming.pptx
Copy of Multithreaded Programming.pptx
 
Recursion & Erlang, FunctionalConf 14, Bangalore
Recursion & Erlang, FunctionalConf 14, BangaloreRecursion & Erlang, FunctionalConf 14, Bangalore
Recursion & Erlang, FunctionalConf 14, Bangalore
 
Async Web Frameworks in Python
Async Web Frameworks in PythonAsync Web Frameworks in Python
Async Web Frameworks in Python
 
Multi t hreading_14_10
Multi t hreading_14_10Multi t hreading_14_10
Multi t hreading_14_10
 
Asynchronous programming
Asynchronous programmingAsynchronous programming
Asynchronous programming
 

More from PyData

More from PyData (20)

Michal Mucha: Build and Deploy an End-to-end Streaming NLP Insight System | P...
Michal Mucha: Build and Deploy an End-to-end Streaming NLP Insight System | P...Michal Mucha: Build and Deploy an End-to-end Streaming NLP Insight System | P...
Michal Mucha: Build and Deploy an End-to-end Streaming NLP Insight System | P...
 
Unit testing data with marbles - Jane Stewart Adams, Leif Walsh
Unit testing data with marbles - Jane Stewart Adams, Leif WalshUnit testing data with marbles - Jane Stewart Adams, Leif Walsh
Unit testing data with marbles - Jane Stewart Adams, Leif Walsh
 
The TileDB Array Data Storage Manager - Stavros Papadopoulos, Jake Bolewski
The TileDB Array Data Storage Manager - Stavros Papadopoulos, Jake BolewskiThe TileDB Array Data Storage Manager - Stavros Papadopoulos, Jake Bolewski
The TileDB Array Data Storage Manager - Stavros Papadopoulos, Jake Bolewski
 
Using Embeddings to Understand the Variance and Evolution of Data Science... ...
Using Embeddings to Understand the Variance and Evolution of Data Science... ...Using Embeddings to Understand the Variance and Evolution of Data Science... ...
Using Embeddings to Understand the Variance and Evolution of Data Science... ...
 
Deploying Data Science for Distribution of The New York Times - Anne Bauer
Deploying Data Science for Distribution of The New York Times - Anne BauerDeploying Data Science for Distribution of The New York Times - Anne Bauer
Deploying Data Science for Distribution of The New York Times - Anne Bauer
 
Graph Analytics - From the Whiteboard to Your Toolbox - Sam Lerma
Graph Analytics - From the Whiteboard to Your Toolbox - Sam LermaGraph Analytics - From the Whiteboard to Your Toolbox - Sam Lerma
Graph Analytics - From the Whiteboard to Your Toolbox - Sam Lerma
 
Do Your Homework! Writing tests for Data Science and Stochastic Code - David ...
Do Your Homework! Writing tests for Data Science and Stochastic Code - David ...Do Your Homework! Writing tests for Data Science and Stochastic Code - David ...
Do Your Homework! Writing tests for Data Science and Stochastic Code - David ...
 
RESTful Machine Learning with Flask and TensorFlow Serving - Carlo Mazzaferro
RESTful Machine Learning with Flask and TensorFlow Serving - Carlo MazzaferroRESTful Machine Learning with Flask and TensorFlow Serving - Carlo Mazzaferro
RESTful Machine Learning with Flask and TensorFlow Serving - Carlo Mazzaferro
 
Mining dockless bikeshare and dockless scootershare trip data - Stefanie Brod...
Mining dockless bikeshare and dockless scootershare trip data - Stefanie Brod...Mining dockless bikeshare and dockless scootershare trip data - Stefanie Brod...
Mining dockless bikeshare and dockless scootershare trip data - Stefanie Brod...
 
Avoiding Bad Database Surprises: Simulation and Scalability - Steven Lott
Avoiding Bad Database Surprises: Simulation and Scalability - Steven LottAvoiding Bad Database Surprises: Simulation and Scalability - Steven Lott
Avoiding Bad Database Surprises: Simulation and Scalability - Steven Lott
 
Words in Space - Rebecca Bilbro
Words in Space - Rebecca BilbroWords in Space - Rebecca Bilbro
Words in Space - Rebecca Bilbro
 
End-to-End Machine learning pipelines for Python driven organizations - Nick ...
End-to-End Machine learning pipelines for Python driven organizations - Nick ...End-to-End Machine learning pipelines for Python driven organizations - Nick ...
End-to-End Machine learning pipelines for Python driven organizations - Nick ...
 
Pydata beautiful soup - Monica Puerto
Pydata beautiful soup - Monica PuertoPydata beautiful soup - Monica Puerto
Pydata beautiful soup - Monica Puerto
 
1D Convolutional Neural Networks for Time Series Modeling - Nathan Janos, Jef...
1D Convolutional Neural Networks for Time Series Modeling - Nathan Janos, Jef...1D Convolutional Neural Networks for Time Series Modeling - Nathan Janos, Jef...
1D Convolutional Neural Networks for Time Series Modeling - Nathan Janos, Jef...
 
Extending Pandas with Custom Types - Will Ayd
Extending Pandas with Custom Types - Will AydExtending Pandas with Custom Types - Will Ayd
Extending Pandas with Custom Types - Will Ayd
 
Measuring Model Fairness - Stephen Hoover
Measuring Model Fairness - Stephen HooverMeasuring Model Fairness - Stephen Hoover
Measuring Model Fairness - Stephen Hoover
 
What's the Science in Data Science? - Skipper Seabold
What's the Science in Data Science? - Skipper SeaboldWhat's the Science in Data Science? - Skipper Seabold
What's the Science in Data Science? - Skipper Seabold
 
Applying Statistical Modeling and Machine Learning to Perform Time-Series For...
Applying Statistical Modeling and Machine Learning to Perform Time-Series For...Applying Statistical Modeling and Machine Learning to Perform Time-Series For...
Applying Statistical Modeling and Machine Learning to Perform Time-Series For...
 
Solving very simple substitution ciphers algorithmically - Stephen Enright-Ward
Solving very simple substitution ciphers algorithmically - Stephen Enright-WardSolving very simple substitution ciphers algorithmically - Stephen Enright-Ward
Solving very simple substitution ciphers algorithmically - Stephen Enright-Ward
 
The Face of Nanomaterials: Insightful Classification Using Deep Learning - An...
The Face of Nanomaterials: Insightful Classification Using Deep Learning - An...The Face of Nanomaterials: Insightful Classification Using Deep Learning - An...
The Face of Nanomaterials: Insightful Classification Using Deep Learning - An...
 

Recently uploaded

Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Victor Rentea
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Victor Rentea
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 

Recently uploaded (20)

Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf
 
Cyberprint. Dark Pink Apt Group [EN].pdf
Cyberprint. Dark Pink Apt Group [EN].pdfCyberprint. Dark Pink Apt Group [EN].pdf
Cyberprint. Dark Pink Apt Group [EN].pdf
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 

Asynchronous Python A Gentle Introduction

  • 1. …….Asynchronous Python: A Gentle Introduction……… James Cropcho, PyData New York 2017
  • 2. Structure of Presentation (1) Define asynchronous programming, thereby disambiguating related but independent concepts (2) Define coroutines (3) Survey Python asynchronous programming implementations (4) First steps towards asynchrony for a program (5) Some asyncio tips I target CPython 3.7 only, and ignore deprecated interfaces. I’m no longer comparing implementations using different libraries, as I’d planned in the talk proposal. Please do not hold questions until the end.
  • 3. You don’t need me around to look up the exact syntax for what you’re trying to do via StackOverflow; I am here to get you to that point, where you are putting down brass tacks. I also aim to provide “evergreen” material which does not become irrelevant rapidly.
  • 4. Processes and Threads Shared memory: Threads, yes. Processes, no. “The principal challenge of multi-threaded applications is coordinating threads that share data or other resources. To that end, the threading module provides a number of synchronization primitives including locks, events, condition variables, and semaphores. …While those tools are powerful, minor design errors can result in problems that are difficult to reproduce.” (Multi-threading) Processes: interprocess communication
  • 5. Concurrent Computing vs. Parallel Computing “Parallel computing is closely related to concurrent computing—they are frequently used together, and often conflated, though the two are distinct: it is possible to have parallelism without concurrency (such as bit-level parallelism), and concurrency without parallelism (such as multitasking by time-sharing on a single-core CPU).[5][6]” (Wikipedia: Parallel Computing)
  • 6. Preemptive vs. Cooperative Multitasking With cooperative multitasking, the code “volunteers” to give up control.
  • 7. Blocking vs non-blocking Blocking: “non-asynchronous” “Can I do something else while I’m waiting?
  • 8. So What is Asynchrony? Generally regarded as a single-threaded programming “style”/”paradigm” with a queue of non-blocking tasks. One thing at a time! Baking bread while doing dishes (state is retained across return to a previous context) So, as commonly stated, is there a speed boost from this?
  • 9. @sixty_north Coroutine Concurrency in Python 3 Getting to grips with asyncio Robert Smallshire @robsmallshire 1
  • 10. Definitions #1 Dealing with multiple things at once versus doing multiple things at once. Concurrency Parallelism Tasks start, run, and complete in overlapping time periods Tasks run simultaneously coroutines 1 threads / processes + multicore
  • 11. Definitions #2 The definition of synchronous contradicts common usage Sequential (Synchronous) Must complete before proceeding Asynchronous No need to wait before proceeding overall duration shorter 1 overall duration longer
  • 12. Definitions #4 Coöperative multitasking Tasks yield to scheduler Preëmptive multitasking Scheduler interrupts tasks Inconvenient context switches 1 Uncooperative tasks hang system
  • 13. Coroutines “Coroutines are computer-program components that generalize subroutines for non-preemptive multitasking, by allowing multiple entry points for suspending and resuming execution at certain locations” (Wikipedia: Coroutines) Subroutines are a subset of coroutines. Generators (semicoroutines) are between the two. Tasklets, generators, async/await vs generator syntax, greenlets… Coroutine vs coroutine objects (Smallshire): code only, callable vs code+state, awaitable
  • 14. Coroutines 2 Things a coroutine can do (18.5 asyncio): ● result = await future or result = yield from future ● result = await coroutine or result = yield from coroutine ● return expression ● raise exception
  • 15. 1 Filter and print coroutine Similar to async_search, but prints all matches def async_print_matches(iterable, for item in iterable: if predicate(item): predicate): print("Found :", item, yield end=", ")
  • 16. Event Loops 1. while 1 (Maxwell): 2. wait for something to happen 3. react to whatever happened …and Executors: Executors are great for if one must really use a synch/blocking library (Langa) Task: making a coroutine into a future, and enqueueing it upon the event loop (asyncio-speak only)
  • 17. I/O aware scheduler Tasks suspend when waiting, scheduled when data ready 1 2 3 4 5 6 7 8waiting on I/O I/O ready scheduled 1
  • 18. Round-robin scheduler Tasks run in circular order 1 2 3 4 5 6 7 8 1
  • 19. Why is This Difficult? “The principal challenge of multi-threaded applications is coordinating threads that share data or other resources.” (11.4 Multi-threading) Subtler feedback/failures Simultaneously concerned with different abstraction/interface/implementation levels, i.e. concerned with implementation in the same space/code as interface AND: this is because it’s cooperative multitasking OR: concerned with stuff going on outside of the thread/context
  • 20. First steps towards asynchrony for a program Scaling: “Processes: tens, Threads: hundreds, Async: thousands” (Grinberg) First ask: is there already an asynchronous package to do this precise thing? aio-libs is very helpful for this! Regardless, asyncio is probably your best bet.
  • 21. Python Asynchronous Implementations asycio is a massive framework encompassing high and low abstraction APIs. Much other tooling is built atop it. Stackless Python: allows “tasklets” which are like coroutines/coroutine-futures Both Eventlet and Gevent use Greenlet. Greenlet is a spin-off of Stackless. “Greenlets are provided as a C extension module for the regular unmodified interpreter.” A “greenlet” is a coroutine. (Greenlet GH) “gevent is inspired by eventlet” (Gevent site) Curio and Trio (new kids on the block), but worth knowing of
  • 22. asyncio Tips PYTHONASYNCIODEBUG=1 python -Wdefault groovy-threads.py “Long CPU-intensive tasks must routinely release the CPU to avoid starving other tasks. This can be done by “sleeping” periodically, such as once per loop iteration. To tell the loop to return control back as soon as possible, sleep for 0 seconds. Example: await asyncio.sleep(0)” (Grinberg) NOTE: When is this less important? Blocking library functions are incompatible with async frameworks (Grinberg) socket.*, select.* subprocess.*, os.waitpid threading.*, multiprocessing.* time.sleep
  • 23. Further Learning: refer to ‘Works Cited’
  • 24. Works [Heavily] Cited and This Work’s License (1) https://commons.wikimedia.org/wiki/File:Open_Make_Up_For_Ever_2013_-_ Team_-_France_-_15.jpg (2) Robert Smallshire (2017) , Getting To Grips With asyncio. Sixty North (3) Demystifying async, await, and asyncio by Sebastiaan Mathôt (4) [18.5. asyncio — Asynchronous I/O, event loop, coroutines and tasks] https://docs.python.org/3.7/library/asyncio.html (5) [11.4. Multi-threading] https://docs.python.org/3.7/tutorial/stdlib2.html#multi-threading (6) Miguel Grinberg, Asynchronous Python for the Complete Beginner Distributed under the Creative Commons Attribution-ShareAlike 4.0 International license, save for Smallshire’s slides.
  • 25. Works [Heavily] Cited 2 (1) Łukasz Langa - Thinking In Coroutines - PyCon 2016 (2) [Maxwell] https://www.quora.com/How-does-an-event-loop-work/answer/Timothy-Maxwell Distributed under the Creative Commons Attribution-ShareAlike 4.0 International license, save for Smallshire’s slides.
  • 26. Question and Answer Period Also… I am seeking pan-industry Python contractor work and a job in finance. Got anything for me?