Asynchronous Python A Gentle Introduction
-
Upload
pydata -
Category
Technology
-
view
126 -
download
17
Transcript of Asynchronous Python A Gentle Introduction
…….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 ThreadsShared 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 MultitaskingWith cooperative multitasking, the code “volunteers” to give up control.
Blocking vs non-blockingBlocking: “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 3Getting to grips with asyncio
Robert Smallshire@robsmallshire
1
Definitions #1Dealing 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
coroutines10
threads / processes
+ multicore
Definitions #2The definition of synchronous contradicts common usage
Sequential (Synchronous)
Must complete before proceeding
Asynchronous
No need to wait before
proceeding
overall duration shorter
11
overall duration longer
Definitions #4
Coöperative multitasking
Tasks yield to scheduler
Preëmptive multitasking
Scheduler interrupts tasks
Inconvenient context switches 1
2
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 2Things 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
Filter and print coroutineSimilar 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 Loops1. while 1 (Maxwell):2. wait for something to happen3. 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
12
3
45
6
7
8waiting on I/O
I/O ready
scheduled
17
Round-robin schedulerTasks run in circular order
12
3
45
6
7
8
18
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 programScaling: “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 Implementationsasycio 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 TipsPYTHONASYNCIODEBUG=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?