Brings event-driven capabilities to Django with a channel system. Django 1.8 and up only.
Project description
This is a work-in-progress code branch of Django implemented as a third-party app, which aims to bring some asynchrony to Django and expand the options for code beyond the request-response model.
The proposal itself is detailed in a very long Gist and there is discussion about it on django-developers.
If you wish to use this in your own project, there’s basic integration instructions below - but be warned! This is not stable and may change massively at any time!
Integration
Make sure you’re running Django 1.8. This doesn’t work with 1.7 (yet?)
If you want to use WebSockets (and that’s kind of the point) you’ll need autobahn and twisted packages too. Python 3/asyncio support coming soon.
pip install channels and then add channels to the TOP of your INSTALLED_APPS list (if it is not at the top you won’t get the new runserver command).
You now have a runserver that actually runs a WSGI interface and a worker in two different threads, runworker to run separate workers, and runwsserver to run a Twisted-based WebSocket server.
You should place consumers in either your views.py or a consumers.py. Here’s an example of WebSocket consumers for basic chat:
import redis from channels import Channel redis_conn = redis.Redis("localhost", 6379) @Channel.consumer("django.websockets.connect") def ws_connect(path, send_channel, **kwargs): redis_conn.sadd("chatroom", send_channel) @Channel.consumer("django.websocket.receive") def ws_receive(channel, send_channel, content, binary, **kwargs): # Ignore binary messages if binary: return # Re-dispatch message for channel in redis_conn.smembers("chatroom"): Channel(channel).send(content=content, binary=False) @Channel.consumer("django.websocket.disconnect") def ws_disconnect(channel, send_channel, **kwargs): redis_conn.srem("chatroom", send_channel) # NOTE: this does not clean up server crash disconnects, # you'd want expiring keys here in real life.
Alternately, you can just push some code outside of a normal view into a worker thread:
from django.shortcuts import render from channels import Channel def my_view(request): # Dispatch a task to run outside the req/response cycle Channel("a_task_channel").send(value=3) # Return a response return render(request, "test.html") @Channel.consumer("a_task_channel") def some_task(channel, value): print "My value was %s from channel %s" % (value, channel)
Limitations
The runserver this command provides currently does not support static media serving, streamed responses or autoreloading.
In addition, this library is a preview and basically might do anything to your code, or change drastically at any time.
If you have opinions, please provide feedback via the appropriate django-developers thread.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
File details
Details for the file channels-0.1.tar.gz
.
File metadata
- Download URL: channels-0.1.tar.gz
- Upload date:
- Size: 11.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | a84225297a94788252d90f152c4c8e70fba19e6dd8ed4f8096c061bacb9952b5 |
|
MD5 | 1256af8706963bd94d7e16a03c7204fe |
|
BLAKE2b-256 | 59f8d5a0e6b7192b02abdb4cef695678a13c67df80128a9571d3adad65cbfa2d |