…….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?

Asynchronous Python A Gentle Introduction

  • 1.
    …….Asynchronous Python: A GentleIntroduction……… 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 needme 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 Sharedmemory: 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. CooperativeMultitasking 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 isAsynchrony? 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 inPython 3 Getting to grips with asyncio Robert Smallshire @robsmallshire 1
  • 10.
    Definitions #1 Dealing withmultiple 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 definitionof 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 yieldto scheduler Preëmptive multitasking Scheduler interrupts tasks Inconvenient context switches 1 Uncooperative tasks hang system
  • 13.
    Coroutines “Coroutines are computer-programcomponents 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 acoroutine 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 printcoroutine 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. while1 (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 Taskssuspend 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 runin circular order 1 2 3 4 5 6 7 8 1
  • 19.
    Why is ThisDifficult? “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 towardsasynchrony 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 asyciois 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: referto ‘Works Cited’
  • 24.
    Works [Heavily] Citedand 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] Cited2 (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 AnswerPeriod Also… I am seeking pan-industry Python contractor work and a job in finance. Got anything for me?