将龙卷风应用程序与django站点集成时的问题

时间:2015-12-23 20:37:29

标签: django tornado

我在Tornado中有一个简单的聊天应用程序,支持RethinkDB。

我试图整合这个龙卷风聊天应用程序与django网站一起运行。

出于这个原因,我在rechat.py中采用了以下内容,以便与django一起使用。

  1. 命名空间tornado.wsgi和django.core.wsgi(get_wsgi_application)
  2. 为Django settings.py设置环境变量 os.environ [' DJANGO_SETTINGS_MODULE'] =' djangoapp.settings'
  3. 当我尝试在上述更改后运行它时,它连接数据库服务器,但没有做任何事情。我错过了什么? 如何让这个龙卷风应用程序与django 1.8网站一起使用?

    以下是我的rechat.py代码(https://github.com/v3ss0n/rechat) -

    import logging
    import tornado.escape
    from tornado.ioloop import IOLoop
    import tornado.web
    import os.path
    import rethinkdb as r
    from tornado import httpserver
    from time import time
    # from tornado.concurrent import Future
    from tornado import gen
    from tornado.options import define, options, parse_command_line
    import tornado.wsgi
    from django.core.wsgi import get_wsgi_application
    
    define("port", default=8888, help="run on the given port", type=int)
    define("debug", default=True, help="run in debug mode")
    
    def setup_db(db_name="rechat", tables=['events']):
        connection = r.connect(host="localhost")
        try:
            r.db_create(db_name).run(connection)
            for tbl in tables:
                r.db(db_name).table_create(tbl, durability="hard").run(connection)
            logging.info('Database setup completed.')
        except r.RqlRuntimeError:
            logging.warn('Database/Table already exists.')
        finally:
            connection.close()
    
    class RechatApp(tornado.web.Application):
        def __init__(self, db):
            handlers = [
                (r"/", MainHandler),
                (r"/a/message/new", MessageNewHandler),
                (r"/a/message/updates", MessageUpdatesHandler),
            ]
            settings = dict(cookie_secret="_asdfasdaasdfasfas",
                            template_path=os.path.join(
                                os.path.dirname(__file__), "templates"),
                            static_path=os.path.join(
                                os.path.dirname(__file__), "static"),
                            xsrf_cookies=True,
                            debug=options.debug)
            self.db = db
            logging.info(db)
            tornado.web.Application.__init__(self, handlers, **settings)
    
    
    class BaseHandler(tornado.web.RequestHandler):
        def initialize(self):
            self.db = self.application.db
            self.evt = r.table("events")
    
    class MainHandler(BaseHandler):
        @gen.coroutine
        def get(self):
            curs = yield self.evt.run(self.db)
            messages = []
            while (yield curs.fetch_next()):
                item = yield curs.next()
                messages.append(item)
    
            self.render("index.html", messages=messages)
    
    
    class MessageNewHandler(BaseHandler):
        @gen.coroutine
        def post(self):
            message = {
                "body": self.get_argument("body")
            }
            # to_basestring is necessary for Python 3's json encoder,
            # which doesn't accept byte strings.
            start = time()
            messages = (yield self.evt.insert(message).run(self.db))
            time_taken = time() - start
            logging.warn("DBINSERT: %s seconds" % time_taken)
            message['id'] = messages['generated_keys'][0]
            message["html"] = tornado.escape.to_basestring(
                self.render_string("message.html", message=message))
            if self.get_argument("next", None):
                self.redirect(self.get_argument("next"))
            else:
                self.write(message)
    
    
    class MessageUpdatesHandler(BaseHandler):
        @gen.coroutine
        def post(self):
            curs = yield self.evt.changes().run(self.db)
    
            while (yield curs.fetch_next()):
                feed = yield curs.next()
                message = {
                    'id': feed['new_val']['id'],
                    'html': tornado.escape.to_basestring(
                        self.render_string("message.html",
                                           message=feed['new_val']))}
                break
            self.finish(dict(messages=[message]))
    
    @gen.coroutine
    def main():
        """ Async main method. It needed to be async due to r.connect is async . """
        parse_command_line()
        os.environ['DJANGO_SETTINGS_MODULE'] = 'djangoapp.settings'
        db_name = "rechat"
        setup_db(db_name)
        r.set_loop_type("tornado")
        db = yield r.connect("localhost", db=db_name)
        #Single db connection for everything thanks a lot Ben and Jeese
        http_server = httpserver.HTTPServer(RechatApp(db))
        http_server.listen(options.port)
    
    if __name__ == "__main__":
        IOLoop.current().run_sync(main)
        IOLoop.current().start()
    

0 个答案:

没有答案