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
Django and this thing called Channels
Search
Andrew Godwin
March 09, 2016
Programming
810
1
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Django and this thing called Channels
A talk I gave at Python SF Meetup, March 2016.
Andrew Godwin
March 09, 2016
More Decks by Andrew Godwin
See All by Andrew Godwin
Reconciling Everything
andrewgodwin
1
400
Django Through The Years
andrewgodwin
0
330
Writing Maintainable Software At Scale
andrewgodwin
0
530
A Newcomer's Guide To Airflow's Architecture
andrewgodwin
0
430
Async, Python, and the Future
andrewgodwin
2
750
How To Break Django: With Async
andrewgodwin
1
830
Taking Django's ORM Async
andrewgodwin
0
850
The Long Road To Asynchrony
andrewgodwin
0
770
The Scientist & The Engineer
andrewgodwin
1
860
Other Decks in Programming
See All in Programming
ソフトウェア設計に溶けるインフラ ― AWS CDK のインフラ認識論
konokenj
3
710
Build-to-own AI: Agentic Development for Humans
inesmontani
PRO
0
130
jsmini JavaScript Engine を作ってみた話
yosuke_furukawa
PRO
0
280
ITヒヤリハットを整理してみた ~ライフサイクルと原因から考える再発防止策~
koukimiura
1
130
Apache Hive: Toward a Cloud Native Lakehouse
okumin
0
170
20260722_microCMSで考える、AI時代のコンテンツ運用設計
yosh1
0
180
AIが無かった頃の素敵な出会いの話
codmoninc
1
300
全PRの83%がAIレビューだけでマージできるようになった開発組織はその後どうなったか
athug
0
650
継続モナドとリアクティブプログラミング
yukikurage
3
670
Claude Team Plan導入・ガイド
tk3fftk
0
240
PHP初心者セッション2026 〜生成AIでは見えない裏側を知る:今だからLAMPを通して仕組みを学ぶ〜
kashioka
0
750
壊れたパーサから始める関数型設計と構成的なパーサ #fp_matsuri
raiga0310
2
420
Featured
See All Featured
Ruling the World: When Life Gets Gamed
codingconduct
0
290
Everyday Curiosity
cassininazir
0
270
How to train your dragon (web standard)
notwaldorf
97
6.7k
Designing for Performance
lara
611
70k
Beyond borders and beyond the search box: How to win the global "messy middle" with AI-driven SEO
davidcarrasco
3
190
StorybookのUI Testing Handbookを読んだ
zakiyama
31
6.9k
Designing Experiences People Love
moore
143
24k
Building a Modern Day E-commerce SEO Strategy
aleyda
45
9.1k
Dominate Local Search Results - an insider guide to GBP, reviews, and Local SEO
greggifford
PRO
0
240
Mobile First: as difficult as doing things right
swwweet
225
10k
Building Applications with DynamoDB
mza
96
7.1k
Building AI with AI
inesmontani
PRO
1
1.1k
Transcript
Andrew Godwin @andrewgodwin DJANGO and this thing called CHANNELS
Andrew Godwin Hi, I'm Django core developer Senior Software Engineer
at Likes complaining about networking
The Problem First: The Solution Then: The Future Finally:
The Problem WSGI ain't so bad
Browser HTTP Webserver Django WSGI View Handler
Browser WebSocket Webserver Django ???? ??? ????
Very hard to deadlock/cause errors Seems "Django-ish" DESIGN GOALS Scales
decently All works inside runserver
Browser WebSocket Webserver Django Channels Consumers Routing
Browser HTTP Webserver Django Channels Consumer Ch. Routing View URL
dispatch
The Solution A Series Of Channels
An ordered, first-in-first-out, at-most once queue with expiry and delivery
to only one listening consumer. Identified by a unique unicode name. WHAT IS A CHANNEL?
Producers Consumers Channel
There are standard channel names: STANDARDS Clients have unique response
channels: http.request websocket.connect websocket.receive !http.response.jaS4kSDj !websocket.send.Qwek120d
There are standard message formats: STANDARDS - HTTP request -
WebSocket close - HTTP response chunk But you can make your own channel names and formats too.
But how do we do this without making Django asynchronous?
Channels are network-transparent, and protocol termination is separate from business
logic process 1 process 2 protocol server worker server channels client socket
Async/cooperative code, but none of yours process 1 process 2
protocol server worker server channels client socket Your logic running synchronously worker server
For runserver, we run them as threads thread 1 thread
2 protocol server worker server channels client socket worker server thread 3 process 1
A view receives a request and sends a single response.
WHAT IS A CONSUMER? A consumer receives a message and sends zero or more messages.
EXAMPLE This echoes messages back: def ws_message(message): text = message.content['text']
reply = "You said: %s" % text message.reply_channel.send({ 'text': reply, })
EXAMPLE This notifies clients of new blog posts: def newpost(message):
for client in client_list: Channel(client).send({ 'text': message.content['id'], })
EXAMPLE But Channels has a better solution for that: #
Channel "websocket.connect" def connect(message): Group('liveblog').add( message.reply_channel) # Channel "new-liveblog" def newpost(message): Group('liveblog').send({ 'text': message.content['id'], })
EXAMPLE And you can send to channels from anywhere: class
LiveblogPost(models.Model): ... def save(self): ... Channel('new-liveblog').send({ 'id': str(self.id), })
EXAMPLE You can offload processing from views: def upload_image(request): ...
Channel('thumbnail').send({ 'id': str(image.id), }) return render(...)
EXAMPLE All the routing is set up like URLs: from
.consumers import ws_connect routing = { 'websocket.connect': ws_connect, 'websocket.receive': 'a.b.receive', 'thumbnail': 'images.consumers.thumb', }
EXAMPLE The HTTP channel just goes to the view system
by default. routing = { # This is actually the default 'http.request': 'channels.asgi.ViewConsumer', }
The Future What does it all mean?
WSGI
WSGI 2?
ASGI channels.readthedocs.org/en/latest/asgi.html
channel_layer.send(channel, message) channel_layer.receive_many(channels) + message standards + extensions (groups, stats)
Daphne HTTP/WebSocket ASGI server asgiref Memory layer, conformance suite, WSGI
adapters asgi_redis Redis-based channel layer
Raw ASGI app example: while True: channel, message = layer.receive_many(
["http.request"], block = True, ) if channel is None: continue layer.send( message['reply_channel'], { 'content': 'Hello World!', 'status': 200, } )
Django 1.10
Django 1.10 And 1.8, 1.9 as a third-party app
It's all optional. No need to change anything if you
don't want to use channels.
pip install channels channels.readthedocs.org
Thanks. Andrew Godwin @andrewgodwin