SlideShare ist ein Scribd-Unternehmen logo
1 von 26
Downloaden Sie, um offline zu lesen
…….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?

Weitere ähnliche Inhalte

Was ist angesagt?

Was ist angesagt? (20)

Golang
GolangGolang
Golang
 
Golang 101
Golang 101Golang 101
Golang 101
 
Async programming and python
Async programming and pythonAsync programming and python
Async programming and python
 
Operating System Chapter 1
Operating System Chapter 1Operating System Chapter 1
Operating System Chapter 1
 
Processes and threads
Processes and threadsProcesses and threads
Processes and threads
 
Python ppt
Python pptPython ppt
Python ppt
 
C++ Standard Template Library
C++ Standard Template LibraryC++ Standard Template Library
C++ Standard Template Library
 
My ppt hpc u4
My ppt hpc u4My ppt hpc u4
My ppt hpc u4
 
Dart workshop
Dart workshopDart workshop
Dart workshop
 
Operating system concepts
Operating system conceptsOperating system concepts
Operating system concepts
 
Introduction to GoLang
Introduction to GoLangIntroduction to GoLang
Introduction to GoLang
 
Go lang
Go langGo lang
Go lang
 
Rust vs C++
Rust vs C++Rust vs C++
Rust vs C++
 
Advance C# Programming Part 1.pptx
Advance C# Programming Part 1.pptxAdvance C# Programming Part 1.pptx
Advance C# Programming Part 1.pptx
 
Async/Await
Async/AwaitAsync/Await
Async/Await
 
Lecture 4 process cpu scheduling
Lecture 4   process cpu schedulingLecture 4   process cpu scheduling
Lecture 4 process cpu scheduling
 
Introduction to Go programming language
Introduction to Go programming languageIntroduction to Go programming language
Introduction to Go programming language
 
C#.NET
C#.NETC#.NET
C#.NET
 
Unit5
Unit5Unit5
Unit5
 
Introducing Async/Await
Introducing Async/AwaitIntroducing Async/Await
Introducing Async/Await
 

Ähnlich wie Asynchronous Python A Gentle Introduction

The Pillars Of Concurrency
The Pillars Of ConcurrencyThe Pillars Of Concurrency
The Pillars Of Concurrency
aviade
 

Ähnlich wie 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
 
Introduction to Python Asyncio
Introduction to Python AsyncioIntroduction to Python Asyncio
Introduction to Python Asyncio
 
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
 

Mehr von PyData

Mehr von 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...
 

Kürzlich hochgeladen

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
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Joaquim Jorge
 

Kürzlich hochgeladen (20)

Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
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
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
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
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation Strategies
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 

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?