Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
The Long Road To Asynchrony
Search
Andrew Godwin
February 22, 2020
Programming
0
580
The Long Road To Asynchrony
My keynote from PyCon Belarus 2020.
Andrew Godwin
February 22, 2020
Tweet
Share
More Decks by Andrew Godwin
See All by Andrew Godwin
Reconciling Everything
andrewgodwin
1
250
Django Through The Years
andrewgodwin
0
150
Writing Maintainable Software At Scale
andrewgodwin
0
380
A Newcomer's Guide To Airflow's Architecture
andrewgodwin
0
300
Async, Python, and the Future
andrewgodwin
2
590
How To Break Django: With Async
andrewgodwin
1
650
Taking Django's ORM Async
andrewgodwin
0
660
The Scientist & The Engineer
andrewgodwin
1
680
Pioneering Real-Time
andrewgodwin
0
340
Other Decks in Programming
See All in Programming
Hotwire or React? ~アフタートーク・本編に含めなかった話~ / Hotwire or React? after talk
harunatsujita
1
120
as(型アサーション)を書く前にできること
marokanatani
10
2.7k
みんなでプロポーザルを書いてみた
yuriko1211
0
280
AWS Lambdaから始まった Serverlessの「熱」とキャリアパス / It started with AWS Lambda Serverless “fever” and career path
seike460
PRO
1
260
最新TCAキャッチアップ
0si43
0
190
距離関数を極める! / SESSIONS 2024
gam0022
0
290
WebフロントエンドにおけるGraphQL(あるいはバックエンドのAPI)との向き合い方 / #241106_plk_frontend
izumin5210
4
1.4k
Laravel や Symfony で手っ取り早く OpenAPI のドキュメントを作成する
azuki
2
120
Jakarta EE meets AI
ivargrimstad
0
240
Flutterを言い訳にしない!アプリの使い心地改善テクニック5選🔥
kno3a87
1
200
subpath importsで始めるモック生活
10tera
0
320
とにかくAWS GameDay!AWSは世界の共通言語! / Anyway, AWS GameDay! AWS is the world's lingua franca!
seike460
PRO
1
900
Featured
See All Featured
個人開発の失敗を避けるイケてる考え方 / tips for indie hackers
panda_program
93
16k
I Don’t Have Time: Getting Over the Fear to Launch Your Podcast
jcasabona
28
2k
Building Applications with DynamoDB
mza
90
6.1k
Building Flexible Design Systems
yeseniaperezcruz
327
38k
Testing 201, or: Great Expectations
jmmastey
38
7.1k
Cheating the UX When There Is Nothing More to Optimize - PixelPioneers
stephaniewalter
280
13k
Evolution of real-time – Irina Nazarova, EuRuKo, 2024
irinanazarova
4
370
10 Git Anti Patterns You Should be Aware of
lemiorhan
655
59k
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
250
21k
No one is an island. Learnings from fostering a developers community.
thoeni
19
3k
Build The Right Thing And Hit Your Dates
maggiecrowley
33
2.4k
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
31
2.7k
Transcript
LONG ROAD ANDREW GODWIN // @andrewgodwin ASYNCHRONY TO THE
Andrew Godwin / @andrewgodwin Hi, I’m Andrew Godwin • Django
core developer • Worked on Migrations, Channels & Async • Once a Londoner, now from Denver, USA
Andrew Godwin / @andrewgodwin
Andrew Godwin / @andrewgodwin "Asynchronous Programming" What is it, really?
Andrew Godwin / @andrewgodwin Concurrent Programming The more general term
Andrew Godwin / @andrewgodwin Input Process Output Sequential execution
Andrew Godwin / @andrewgodwin Input Process Output Process Concurrent execution
Archive
Andrew Godwin / @andrewgodwin Shared use of a single resource
In this case, CPUs
Andrew Godwin / @andrewgodwin "Communicating Sequential Processes", C. A. R
Hoare
Andrew Godwin / @andrewgodwin func main() { messages := make(chan
string) go func() { messages <- "ping" }() msg := <-messages fmt.Println(msg) }
Andrew Godwin / @andrewgodwin Multiple processes multiprocessing Threads threading Event
loops asyncio / twisted
Andrew Godwin / @andrewgodwin Multiple processes scale best It's also
difficult and costs the most!
Andrew Godwin / @andrewgodwin Threads are unpredictable Also, the GIL
is our ever-present friend
Andrew Godwin / @andrewgodwin Event loops are a good compromise
They do require shared memory, though.
Andrew Godwin / @andrewgodwin Asynchronous ≈ Event loops Most of
the time!
Andrew Godwin / @andrewgodwin # Ready when a timer finishes
await asyncio.sleep(1) # Ready when network packets return await client.get("http://example.com") # Ready when the coroutine exits await my_function("hello", 64.2)
Andrew Godwin / @andrewgodwin Network/timer updates An event loop's flow
Select a ready task Run task Add new tasks to queue await
Andrew Godwin / @andrewgodwin Coroutines Time →
Andrew Godwin / @andrewgodwin How did we get here?
Andrew Godwin / @andrewgodwin 1998 threading module, Stackless Python 2002
Twisted 2006 Greenlets (later gevent, eventlet) 2008 multiprocessing module 2012 Tulip, PEP 3156 2014 asyncio module 2005 Coroutine-friendly generators (PEP 342)
Andrew Godwin / @andrewgodwin 2017 Django Channels 1.0 2018 Django
Channels 2.0 2019 DEP 9 (Async support) 2020 Async views land in Django
Andrew Godwin / @andrewgodwin No solution is perfect Everyone chooses
different tradeoffs
Andrew Godwin / @andrewgodwin Asyncio is based on yield from
Because it was prototyped in Python 2
Andrew Godwin / @andrewgodwin Can't tell if a function returns
a coroutine! There are standard hints, but no actual guaranteed way
Andrew Godwin / @andrewgodwin async def calculate(x): result = await
coroutine(x) return result # These both return a coroutine def calculate(x): result = coroutine(x) return result
Andrew Godwin / @andrewgodwin Can't have one function service both
How we got here makes sense, but it's still annoying sometimes.
Andrew Godwin / @andrewgodwin # Calls get.__call__ instance = MyModel.objects.get(id=3)
# Calls get.__call__ # and then awaits its result instance = await MyModel.objects.get(id=3)
Andrew Godwin / @andrewgodwin You have to namespace async functions
I really, really wish we didn't have to
Andrew Godwin / @andrewgodwin instance = MyModel.objects.get(id=3) instance = await
MyModel.objects.async.get(id=3)
Andrew Godwin / @andrewgodwin Completely different libraries! Even sleep() is
different.
Andrew Godwin / @andrewgodwin time.sleep ➞ asyncio.sleep requests ➞ httpx
psycopg2 ➞ aiopg WSGI ➞ ASGI Django ➞ Django?
Andrew Godwin / @andrewgodwin Django & Async
Andrew Godwin / @andrewgodwin Asyncio only benefits IO-bound code Code
that thrashes the CPU doesn't benefit at all
Andrew Godwin / @andrewgodwin We're adding async to some parts
The bits where it makes sense!
Andrew Godwin / @andrewgodwin But, you can't mix sync and
async So we have to have two parallel request paths
Andrew Godwin / @andrewgodwin WSGIHandler __call__ WSGI Server WSGIRequest BaseHandler
get_response URLs Middleware View __call__ HTTP protocol Socket handling Transfer encodings Headers-to-META Upload file wrapping GET/POST parsing Exception catching Atomic view wrapper Django 3.0 Request Flow
Andrew Godwin / @andrewgodwin WSGIHandler __call__ WSGI Server WSGIRequest BaseHandler
get_response URLs Middleware Async View __call__ ASGIHandler __call__ ASGI Server ASGIRequest Sync View __call__ Asynchronous request path Proposed async request flow
Andrew Godwin / @andrewgodwin WSGIHandler __call__ WSGI Server WSGIRequest URLs
Middleware View __call__ ASGIHandler __call__ ASGI Server ASGIRequest Asynchronous request path BaseHandler get_response_async BaseHandler get_response URLs Middleware Async View __call__ Implemented async request flow
Andrew Godwin / @andrewgodwin We have to work with what
we have I'd rather let people ship code than argue about perfection.
Andrew Godwin / @andrewgodwin Django's main job is safety It
matters more than anything else
Andrew Godwin / @andrewgodwin Deadlocks Livelocks Starvation Race conditions
Andrew Godwin / @andrewgodwin Coroutines & the GIL actually help!
You're saved from all the awful memory corruption bugs
Andrew Godwin / @andrewgodwin async def transfer_money(p1, p2): await get_lock()
# Code between awaits is atomic! subtract_money(p1) add_money(p2) await release_lock()
Andrew Godwin / @andrewgodwin You can still screw up a
lot Trust me, I have lived it while developing async
Andrew Godwin / @andrewgodwin async def log_message(m): await client.post("log-server", m)
result = calculate_result() log_message(m)
Andrew Godwin / @andrewgodwin async def log_message(m): await client.post("log-server", m)
result = calculate_result() await log_message(m)
Andrew Godwin / @andrewgodwin Async usability has a long way
to go But it is undoubtedly the future!
Andrew Godwin / @andrewgodwin Python is the language of pragmatism
If anyone can get it right, we can
Andrew Godwin / @andrewgodwin What does the future hold? Hopefully,
no GIL!
Andrew Godwin / @andrewgodwin Let's make async understandable Almost every
project could benefit, if we made it worth their time.
Thanks. Andrew Godwin @andrewgodwin // aeracode.org