Giter VIP home page Giter VIP logo

django-on-tornado's Introduction

Requirements

  • Django_
  • Tornado_

.. _Django: http://www.djangoproject.com/ .. _Tornado: http://www.tornadoweb.org/

Instructions

  1. cd myproject
  2. python manage.py runtornado --reload 8000
  3. Go to: http://localhost:8000/

Now you have a working chat server -- this is based on the NodeJS chat server, UI primarily.

Quick Tutorial

Django request/response is untouched. To utilize Tornado's async capabilities, a decorator is avilable. Which adds an extra argument to your request handler, which is the Tornado handler class -- all functions are available.

If you return a value it is assumed to be a Django Response, if nothing is returned then it is assumed that you're doing async response processing.

from django_tornado.decorator import asynchronous

@asynchronous
def recv(request, handler) :
    response = {}

    if 'since' not in request.GET :
        return ChatResponseError('Must supply since parameter')
    if 'id' not in request.GET :
        return ChatResponseError('Must supply id parameter')

    id = request.GET['id']
    session = Session.get(id)
    if session :
        session.poke()

    since = int(request.GET['since'])

    def on_new_messages(messages) :
        if handler.request.connection.stream.closed():
            return
        handler.finish({ 'messages': messages, 'rss' : channel.size() })

    channel.query(handler.async_callback(on_new_messages), since)

Advanced Usage

Since Tornado is a single threaded server which blocks on long operations (e.g. slow DB queries) it''s useful to run multiple servers at the same time. That''s now supported on the command line, for

 python manage.py runtornado 8000 8001 8002 8003

Which enables the following nginx configuration (skipping lots of details)

upstream frontends {
    server 127.0.0.1:8000;
    server 127.0.0.1:8001;
    server 127.0.0.1:8002;
    server 127.0.0.1:8003;
}

...

location / {
    proxy_pass http://frontends;
}

Acknowledgements

Idea and code snippets borrowed from http://geekscrap.com/2010/02/integrate-tornado-in-django/ Chat server http://github.com/ry/node_chat

django-on-tornado's People

Contributors

rmax avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.