Skip to main content
Help us improve PyPI by participating in user testing. All experience levels needed!

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

History Node

2.1.2

History Node

2.1.1

History Node

2.1.0

History Node

2.0.2

History Node

2.0.1

History Node

2.0.0

History Node

1.1.8

History Node

1.1.7

History Node

1.1.6

History Node

1.1.5

History Node

1.1.4

History Node

1.1.3

History Node

1.1.2

History Node

1.1.1

History Node

1.1.0

History Node

1.0.3

History Node

1.0.2

History Node

1.0.1

History Node

1.0.0

History Node

0.17.3

History Node

0.17.2

History Node

0.17.1

History Node

0.17.0

History Node

0.16.1

History Node

0.16.0

History Node

0.15.1

History Node

0.15.0

History Node

0.14.3

History Node

0.14.2

History Node

0.14.1

History Node

0.14.0

History Node

0.13.1

History Node

0.13.0

History Node

0.12.0

History Node

0.11.0

History Node

0.10.3

History Node

0.10.2

History Node

0.10.1

History Node

0.10.0

History Node

0.9.5

History Node

0.9.4

History Node

0.9.3

History Node

0.9.2

History Node

0.9.1

History Node

0.9

History Node

0.8

History Node

0.7

History Node

0.1.1

This version
History Node

0.1

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Filename, size & hash SHA256 hash help File type Python version Upload date
channels-0.1.tar.gz (11.1 kB) Copy SHA256 hash SHA256 Source None Jun 15, 2015

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging CloudAMQP CloudAMQP RabbitMQ AWS AWS Cloud computing Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page