SlideShare a Scribd company logo
1 of 62
Download to read offline
ASYNC TASKS WITH
DJANGO CHANNELS
PyCon Canada 2016
Albert O’Connor
@amjoconn
albertoconnor.ca
What is Channels?
The most exciting thing to
happen to Django since Django
Warnings
I won’t be talking about
WebSockets
The Channels Docs have a good intro tutorial on WebSockets
https://channels.readthedocs.io/en/stable/getting-started.html
I expect you are familiar with
Django
So, Channels is…
A library which allows Django to handle
more than just plain HTTP Requests
Created by Andrew Godwin
Supported by a Mozilla MOSS Grant
An “official” Django project
Problem:
How to support WebSockets
in Django
Solution:
Another level of abstraction
Django
Receives a HTTP Request, calls a view
which returns an HTTP Response
Channels
Receives a message, calls a consumer
which can send other messages
Django Channels
Receive a HTTP Request message,
calls a consumer which calls a view
The view returns a HTTP Response, the
consumer send the message to the
http.response channel
Abstraction!
browser
WSGI
HTTP Request
Django
your view
HTTP Response
browser
ASGI
HTTP Request
Channels
your view
HTTP Response
view_consumer
browser
ASGI
HTTP Request
Channels
your view
HTTP Response
view_consumer
messagehttp.request message http.response!foo
Django HTTP Request Django HTTP Response
There are WebSocket
specific channels including
websocket.connect and
websocket.receive
But…
You can create your own channel!
Channels are named queues
on a message bus
We are going to create a channel to
deal with an async background task
Warning
At-most-once delivery
Ordering is also worth thinking about:
https://channels.readthedocs.io/en/stable/getting-started.html#enforcing-ordering
Quick Examples
How do you add
Channels to your project?
Pip install channels and add
it to INSTALLED_APPS
Channels “replaces” WSGI with ASGI
Installing Channels includes a
ASGI server called Daphne
implemented with Twisted
Django gains a runworker
management command
For development runserver works by
running workers and Daphne in one
process using threads
For production an ASGI broker is needed
between Daphne and the workers
asgi_redis + redis server
is a great option
Daphne handles HTTP,
WebSockets, and more, enqueuing
messages into the right channel
Views and consumers can also
enqueue messages into channels
This means your view and consumer
code is written synchronously
https://www.flickr.com/photos/moonlightbulb/3338852116
Demo
https://github.com/albertoconnor/asyncdemo
Tag: step1
Basic Django app with a view which says
hello and simulates sending a notification
# In hello/views.py
def delay():
while True:
for i in [5, 5, 5, 30]: # Simulate unreliability
yield i
delay_generator = delay()
def send_notification(message):
time.sleep(next(delay_generator))
print(message) # Simulate sending to slack etc.
def hello_view(request, template="hello.html"):
name = request.GET.get('name', 'World')
message = 'Hello, {}!'.format(name)
send_notification(message)
return render(
request,
template,
dict(message=message),
)
Tag: step2
Install Channels and update settings
# In requirements.txt
django==1.10.2
channels==0.17.3
Successfully installed
asgiref-0.14.0
autobahn-0.16.0
channels-0.17.3
daphne-0.15.0
six-1.10.0
twisted-16.4.1
txaio-2.5.1
zope.interface-4.3.2
# In asyncdemo/settings.py
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
...
'channels',
)
CHANNEL_LAYERS = {
"default": {
"BACKEND": "asgiref.inmemory.ChannelLayer",
"ROUTING": "asyncdemo.routing.channel_routing",
},
}
# In routing.py
from channels.routing import route
channel_routing = []
Performing system checks...
System check identified no issues (0 silenced).
November 10, 2016 - 11:43:12
Django version 1.10.2, using settings 'asyncdemo.settings'
Starting Channels development server at http://127.0.0.1:8001/
Channel layer default (asgiref.inmemory.ChannelLayer)
Quit the server with CONTROL-C.
2016-11-10 11:43:12,340 - INFO - worker - Listening on channels http.request, websocket.connect, websocket.receive
2016-11-10 11:43:12,340 - INFO - worker - Listening on channels http.request, websocket.connect, websocket.receive
2016-11-10 11:43:12,341 - INFO - worker - Listening on channels http.request, websocket.connect, websocket.receive
2016-11-10 11:43:12,341 - INFO - worker - Listening on channels http.request, websocket.connect, websocket.receive
2016-11-10 11:43:12,347 - INFO - server - Using busy-loop synchronous mode on channel layer
Tag: step3
Create channel and use it
# In hello/views.py
from django.shortcuts import render
from channels import Channel
def hello_view(request, template="hello.html"):
name = request.GET.get('name', 'World')
message = 'Hello, {}!'.format(name)
Channel('notify').send(
dict(
message=message,
)
)
return render(
request,
template,
dict(message=message),
)
# In asyncdemo/routing.py
from channels.routing import route
from hello import consumers
channel_routing = [
route('notify', consumers.notify),
]
# In hello/consumers.py
import time
def delay():
while True:
for i in [5, 5, 5, 30]:
yield i
delay_generator = delay()
def notify(message):
time.sleep(next(delay_generator))
print(message['message'])
Now the website is responsive
until it gets backed up
Tag: bonusround
Use redis, Daphne and
run separate processes
# In requirements.txt
django==1.10.2
channels==0.17.3
asgi_redis==0.14.1 # Also need redis running
# In asyncdemo/settings.py
CHANNEL_LAYERS = {
"default": {
"BACKEND": "asgi_redis.RedisChannelLayer",
"CONFIG": {
"hosts": ['redis://localhost:6379'],
},
"ROUTING": "asyncdemo.routing.channel_routing",
},
}
This should be enough to get
runserver working again
To use Daphne we need to
create asgi.py similar to wsgi.py
# In asyncdemo/asgi.py
import os
import channels.asgi
os.environ.setdefault(
"DJANGO_SETTINGS_MODULE",
"asyncdemo.settings"
)
channel_layer = channels.asgi.get_channel_layer()
daphne asyncdemo.asgi:channel_layer --port 8000
Now we need some workers
python manage.py runworker
python manage.py runworker --exclude-channels=notify
That’s it!
Now go forth and write
readable async code
ASYNC TASKS WITH
DJANGO CHANNELS
This was…
Thanks!
Questions?
PyCon Canada 2016
Albert O’Connor
@amjoconn
albertoconnor.ca

More Related Content

What's hot

SLF4J (Simple Logging Facade for Java)
SLF4J (Simple Logging Facade for Java)SLF4J (Simple Logging Facade for Java)
SLF4J (Simple Logging Facade for Java)
Guo Albert
 
Apache Karaf - Building OSGi applications on Apache Karaf - T Frank & A Grzesik
Apache Karaf - Building OSGi applications on Apache Karaf - T Frank & A GrzesikApache Karaf - Building OSGi applications on Apache Karaf - T Frank & A Grzesik
Apache Karaf - Building OSGi applications on Apache Karaf - T Frank & A Grzesik
mfrancis
 
Event Streaming with Kafka Streams and Spring Cloud Stream | Soby Chacko, VMware
Event Streaming with Kafka Streams and Spring Cloud Stream | Soby Chacko, VMwareEvent Streaming with Kafka Streams and Spring Cloud Stream | Soby Chacko, VMware
Event Streaming with Kafka Streams and Spring Cloud Stream | Soby Chacko, VMware
HostedbyConfluent
 
Salty OPS – Saltstack Introduction
Salty OPS – Saltstack IntroductionSalty OPS – Saltstack Introduction
Salty OPS – Saltstack Introduction
Walter Liu
 

What's hot (20)

Flask – Python
Flask – PythonFlask – Python
Flask – Python
 
Logstash
LogstashLogstash
Logstash
 
Fastapi
FastapiFastapi
Fastapi
 
Bitbucket and Git
Bitbucket and GitBitbucket and Git
Bitbucket and Git
 
BitBucket presentation
BitBucket presentationBitBucket presentation
BitBucket presentation
 
Know your app: Add metrics to Java with Micrometer | DevNation Tech Talk
Know your app: Add metrics to Java with Micrometer | DevNation Tech TalkKnow your app: Add metrics to Java with Micrometer | DevNation Tech Talk
Know your app: Add metrics to Java with Micrometer | DevNation Tech Talk
 
Web develop in flask
Web develop in flaskWeb develop in flask
Web develop in flask
 
Intro to Git, GitHub, and BitBucket
Intro to Git, GitHub, and BitBucketIntro to Git, GitHub, and BitBucket
Intro to Git, GitHub, and BitBucket
 
SLF4J (Simple Logging Facade for Java)
SLF4J (Simple Logging Facade for Java)SLF4J (Simple Logging Facade for Java)
SLF4J (Simple Logging Facade for Java)
 
Jenkins vs GitLab CI
Jenkins vs GitLab CIJenkins vs GitLab CI
Jenkins vs GitLab CI
 
github-actions.pdf
github-actions.pdfgithub-actions.pdf
github-actions.pdf
 
Apache Karaf - Building OSGi applications on Apache Karaf - T Frank & A Grzesik
Apache Karaf - Building OSGi applications on Apache Karaf - T Frank & A GrzesikApache Karaf - Building OSGi applications on Apache Karaf - T Frank & A Grzesik
Apache Karaf - Building OSGi applications on Apache Karaf - T Frank & A Grzesik
 
Retrofit library for android
Retrofit library for androidRetrofit library for android
Retrofit library for android
 
[MOPCON 2022] 以 Kotlin Multiplatform 制霸全平台
[MOPCON 2022] 以 Kotlin Multiplatform 制霸全平台[MOPCON 2022] 以 Kotlin Multiplatform 制霸全平台
[MOPCON 2022] 以 Kotlin Multiplatform 制霸全平台
 
Quick flask an intro to flask
Quick flask   an intro to flaskQuick flask   an intro to flask
Quick flask an intro to flask
 
Bitbucket
BitbucketBitbucket
Bitbucket
 
Event Streaming with Kafka Streams and Spring Cloud Stream | Soby Chacko, VMware
Event Streaming with Kafka Streams and Spring Cloud Stream | Soby Chacko, VMwareEvent Streaming with Kafka Streams and Spring Cloud Stream | Soby Chacko, VMware
Event Streaming with Kafka Streams and Spring Cloud Stream | Soby Chacko, VMware
 
Git & GitHub WorkShop
Git & GitHub WorkShopGit & GitHub WorkShop
Git & GitHub WorkShop
 
Git & GitHub for Beginners
Git & GitHub for BeginnersGit & GitHub for Beginners
Git & GitHub for Beginners
 
Salty OPS – Saltstack Introduction
Salty OPS – Saltstack IntroductionSalty OPS – Saltstack Introduction
Salty OPS – Saltstack Introduction
 

Viewers also liked

Dive into Python Class
Dive into Python ClassDive into Python Class
Dive into Python Class
Jim Yeh
 

Viewers also liked (20)

Django channels
Django channelsDjango channels
Django channels
 
Real time web_apps_pycon2012-v1
Real time web_apps_pycon2012-v1Real time web_apps_pycon2012-v1
Real time web_apps_pycon2012-v1
 
Real-Time Django
Real-Time DjangoReal-Time Django
Real-Time Django
 
Django for IoT: From hackathon to production (DjangoCon US)
Django for IoT: From hackathon to production (DjangoCon US)Django for IoT: From hackathon to production (DjangoCon US)
Django for IoT: From hackathon to production (DjangoCon US)
 
Faster Python, FOSDEM
Faster Python, FOSDEMFaster Python, FOSDEM
Faster Python, FOSDEM
 
Dive into Python Class
Dive into Python ClassDive into Python Class
Dive into Python Class
 
Python on Rails 2014
Python on Rails 2014Python on Rails 2014
Python on Rails 2014
 
Python class
Python classPython class
Python class
 
Implementation of RSA Algorithm for Speech Data Encryption and Decryption
Implementation of RSA Algorithm for Speech Data Encryption and DecryptionImplementation of RSA Algorithm for Speech Data Encryption and Decryption
Implementation of RSA Algorithm for Speech Data Encryption and Decryption
 
Django Introduction & Tutorial
Django Introduction & TutorialDjango Introduction & Tutorial
Django Introduction & Tutorial
 
A deep dive into PEP-3156 and the new asyncio module
A deep dive into PEP-3156 and the new asyncio moduleA deep dive into PEP-3156 and the new asyncio module
A deep dive into PEP-3156 and the new asyncio module
 
The future of async i/o in Python
The future of async i/o in PythonThe future of async i/o in Python
The future of async i/o in Python
 
Python, do you even async?
Python, do you even async?Python, do you even async?
Python, do you even async?
 
Comandos para ubuntu 400 que debes conocer
Comandos para ubuntu 400 que debes conocerComandos para ubuntu 400 que debes conocer
Comandos para ubuntu 400 que debes conocer
 
Python master class 3
Python master class 3Python master class 3
Python master class 3
 
The State of WebSockets in Django
The State of WebSockets in DjangoThe State of WebSockets in Django
The State of WebSockets in Django
 
Python Async IO Horizon
Python Async IO HorizonPython Async IO Horizon
Python Async IO Horizon
 
Django Celery
Django Celery Django Celery
Django Celery
 
Practical continuous quality gates for development process
Practical continuous quality gates for development processPractical continuous quality gates for development process
Practical continuous quality gates for development process
 
The Awesome Python Class Part-4
The Awesome Python Class Part-4The Awesome Python Class Part-4
The Awesome Python Class Part-4
 

Similar to Async Tasks with Django Channels

Basic html5 and javascript
Basic html5 and javascriptBasic html5 and javascript
Basic html5 and javascript
wendy017
 

Similar to Async Tasks with Django Channels (20)

Play Framework: async I/O with Java and Scala
Play Framework: async I/O with Java and ScalaPlay Framework: async I/O with Java and Scala
Play Framework: async I/O with Java and Scala
 
video conference (peer to peer)
video conference (peer to peer)video conference (peer to peer)
video conference (peer to peer)
 
flask.pptx
flask.pptxflask.pptx
flask.pptx
 
Performance as User Experience [An Event Apart Denver 2017]
Performance as User Experience [An Event Apart Denver 2017]Performance as User Experience [An Event Apart Denver 2017]
Performance as User Experience [An Event Apart Denver 2017]
 
Performance as User Experience [AEA SEA 2018]
Performance as User Experience [AEA SEA 2018]Performance as User Experience [AEA SEA 2018]
Performance as User Experience [AEA SEA 2018]
 
Building ColdFusion And AngularJS Applications
Building ColdFusion And AngularJS ApplicationsBuilding ColdFusion And AngularJS Applications
Building ColdFusion And AngularJS Applications
 
Basic html5 and javascript
Basic html5 and javascriptBasic html5 and javascript
Basic html5 and javascript
 
The Play Framework at LinkedIn
The Play Framework at LinkedInThe Play Framework at LinkedIn
The Play Framework at LinkedIn
 
The Play Framework at LinkedIn: productivity and performance at scale - Jim B...
The Play Framework at LinkedIn: productivity and performance at scale - Jim B...The Play Framework at LinkedIn: productivity and performance at scale - Jim B...
The Play Framework at LinkedIn: productivity and performance at scale - Jim B...
 
Jwebsocketmobiletechcon2010en 100912071225 Phpapp01
Jwebsocketmobiletechcon2010en 100912071225 Phpapp01Jwebsocketmobiletechcon2010en 100912071225 Phpapp01
Jwebsocketmobiletechcon2010en 100912071225 Phpapp01
 
jWebSocket MobileTechCon 2010 - WebSockets on Android, Symbian and BlackBerry
jWebSocket MobileTechCon 2010 - WebSockets on Android, Symbian and BlackBerryjWebSocket MobileTechCon 2010 - WebSockets on Android, Symbian and BlackBerry
jWebSocket MobileTechCon 2010 - WebSockets on Android, Symbian and BlackBerry
 
Rails missing features
Rails missing featuresRails missing features
Rails missing features
 
Front End Development for Back End Developers - Devoxx UK 2017
 Front End Development for Back End Developers - Devoxx UK 2017 Front End Development for Back End Developers - Devoxx UK 2017
Front End Development for Back End Developers - Devoxx UK 2017
 
Building Mobile Friendly APIs in Rails
Building Mobile Friendly APIs in RailsBuilding Mobile Friendly APIs in Rails
Building Mobile Friendly APIs in Rails
 
Implementing Web Services In Java
Implementing Web Services In JavaImplementing Web Services In Java
Implementing Web Services In Java
 
Implementing Comet using PHP
Implementing Comet using PHPImplementing Comet using PHP
Implementing Comet using PHP
 
WEB BROWSER
WEB BROWSERWEB BROWSER
WEB BROWSER
 
REST in peace @ IPC 2012 in Mainz
REST in peace @ IPC 2012 in MainzREST in peace @ IPC 2012 in Mainz
REST in peace @ IPC 2012 in Mainz
 
Html intake 38 lect1
Html intake 38 lect1Html intake 38 lect1
Html intake 38 lect1
 
Web Apps
Web AppsWeb Apps
Web Apps
 

Recently uploaded

CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
mohitmore19
 

Recently uploaded (20)

A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docx
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.com
 
LEVEL 5 - SESSION 1 2023 (1).pptx - PDF 123456
LEVEL 5   - SESSION 1 2023 (1).pptx - PDF 123456LEVEL 5   - SESSION 1 2023 (1).pptx - PDF 123456
LEVEL 5 - SESSION 1 2023 (1).pptx - PDF 123456
 
Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdf
Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdfAzure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdf
Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdf
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
 
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learn
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
 
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
 
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park %in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
 
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
 
%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
 
8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students
 
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
 

Async Tasks with Django Channels