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, Channels, and Distributed Systems
Search
Sponsored
·
SiteGround - Reliable hosting with speed, security, and support you can count on.
→
Andrew Godwin
August 21, 2016
Programming
740
1
Share
Django, Channels, and Distributed Systems
A talk I gave at PyBay 2016
Andrew Godwin
August 21, 2016
More Decks by Andrew Godwin
See All by Andrew Godwin
Reconciling Everything
andrewgodwin
1
380
Django Through The Years
andrewgodwin
0
300
Writing Maintainable Software At Scale
andrewgodwin
0
510
A Newcomer's Guide To Airflow's Architecture
andrewgodwin
0
410
Async, Python, and the Future
andrewgodwin
2
730
How To Break Django: With Async
andrewgodwin
1
800
Taking Django's ORM Async
andrewgodwin
0
790
The Long Road To Asynchrony
andrewgodwin
0
750
The Scientist & The Engineer
andrewgodwin
1
830
Other Decks in Programming
See All in Programming
ソフトウェア設計の結合バランス #phperkaigi
kajitack
0
140
クラウドネイティブなエンジニアに向ける Raycastの魅力と実際の活用事例
nealle
2
210
YJITとZJITにはイカなる違いがあるのか?
nakiym
0
240
TiDBのアーキテクチャから学ぶ分散システム入門 〜MySQL互換のNewSQLは何を解決するのか〜 / tidb-architecture-study
dznbk
1
180
PDI: Como Alavancar Sua Carreira e Seu Negócio
marcelgsantos
0
130
How Swift's Type System Guides AI Agents
koher
0
290
一度始めたらやめられない開発効率向上術 / Findy あなたのdotfilesを教えて!
k0kubun
4
3k
The Monolith Strikes Back: Why AI Agents ❤️ Rails Monoliths
serradura
0
340
運転動画を検索可能にする〜Cosmos-Embed1とDatabricks Vector Searchで〜/cosmos-embed1-databricks-vector-search
studio_graph
0
380
煩雑なSkills管理をSoC(関心の分離)により解決する――関心を分離し、プロンプトを部品として育てるためのOSSを作った話 / Solving Complex Skills Management Through SoC (Separation of Concerns)
nrslib
4
980
SkillがSkillを生む:QA観点出しを自動化した
sontixyou
6
3.4k
tRPCの概要と少しだけパフォーマンス
misoton665
2
220
Featured
See All Featured
Building Applications with DynamoDB
mza
96
7k
A Soul's Torment
seathinner
6
2.7k
The Illustrated Children's Guide to Kubernetes
chrisshort
51
52k
The Pragmatic Product Professional
lauravandoore
37
7.2k
The Director’s Chair: Orchestrating AI for Truly Effective Learning
tmiket
1
150
Improving Core Web Vitals using Speculation Rules API
sergeychernyshev
21
1.4k
Fashionably flexible responsive web design (full day workshop)
malarkey
408
66k
Leveraging Curiosity to Care for An Aging Population
cassininazir
1
220
Practical Tips for Bootstrapping Information Extraction Pipelines
honnibal
25
1.9k
AI in Enterprises - Java and Open Source to the Rescue
ivargrimstad
0
1.2k
Connecting the Dots Between Site Speed, User Experience & Your Business [WebExpo 2025]
tammyeverts
11
890
Being A Developer After 40
akosma
91
590k
Transcript
None
Andrew Godwin Hi, I'm Django core developer Senior Software Engineer
at Used to complain about migrations a lot
Channels
Born from WebSockets
Born from WebSockets Expanded to be more
The "real hard problem"
Asynchronous coordination
You take a request... ...and return a response.
HTTP 1 request response Browser Server request response request response
request response
HTTP 2 request response Browser Server request response request 1
response 2 request 2 response 1
WebSockets receive send Browser Server send receive send send receive
???
Server Client 1 Client 2 Client 3 Client 4
Server Client 1 Client 2 Client 3 Client 4 Server
Server Client 1 Client 2 Client 3 Client 4 Server
The "hard problem"
Broadcast
We need to coordinate between servers
Channels is a foundation for runnng 'async' at scale
Architecture is about tradeoffs
At most once / At least once Ordered / Unordered
FIFO / FILO Expiry / Persistence
WebSockets Service-oriented Architecture Chat/Email integration IoT protocols
What makes it hard?
DISTRIBUTED SYSTEMS
Stateful connections Internal network Bottlenecks Machines dying
Server Server Django Script ASGI "Send to channel X" "Receive
from channel X" "Send to group Y" "Add channel X to group Y" "Remove channel X from group Y"
Server Server Django Script ASGI ASGI ASGI ASGI Redis
Server Server Django Script ASGI ASGI ASGI ASGI Shared Memory
Server Server Django Script ASGI ASGI ASGI ASGI Shared Memory
Redis Redis
bit.ly/asgi-spec
Channels wraps the low-level ASGI operations
Think of it as the "Django bit" of a larger
whole.
Daphne HTTP/WebSocket Server Channels Django integration asgi-redis Redis backend asgi-ipc
Local memory backend asgiref Shared code and libs
What does Channels provide? Routing Consumers Sessions Auth Helpers By
channel, URL, etc. Standardised message handling Cross-network persistence on sockets Including HTTP cookies on WebSocket runserver, runworker, debugging info
Putting it to use Let's make a chat!
Consumers def on_connect(message): Group("chat").add(message.reply_channel) def on_receive(message): Group("chat").send({"text": message["text"]}) def on_disconnect(message):
Group("chat").discard(message.reply_channel) websocket.connect websocket.receive websocket.disconnect
Routing from channels import route routing = [ route("websocket.connect", on_connect),
route("websocket.receive", on_receive), route("websocket.disconnect", on_disconnect) ]
Class-based from channels import route_class routing = [ route_class(ChatConsumer), ]
from channels.generic.websockets class ChatConsumer(WebsocketConsumer): def connection_groups(self): return ["chat"] def receive(self, text): self.group_send("chat", text=text) Routing
Full worked example github.com/andrewgodwin/channels-examples
Ignoring Django
1. Take a channel layer daphne myproject.asgi:channel_layer
2. Tie it into an event loop Twisted, asyncio, or
while-True
3. Call send/receive It's a communication channel!
Example: SOA Services receive()-block waiting for tasks Clients use send()
with a reply-channel to call an endpoint Servers process and send() the reply
IRC Worker ASGI Worker Worker Email MQTT Scheduler HTTP/WS Custom
Daemon
Channels is a tool for you to use
There's more to be done (and some funding for it)
The tradeoffs may not be for you! (Especially as you
specialise)
1995 You are a desktop app 2005 You are a
website 2015 You are a rich web/mobile app 2025 ?
What are the goals of a framework? Do we adapt?
Thanks. Andrew Godwin @andrewgodwin channels.readthedocs.io github.com/andrewgodwin/channels-examples