Twisted API for Couchbase not working with Python Tornado

不羁岁月 提交于 2020-01-03 20:11:09

问题


I'm trying to run a Tornado server with Couchbase 4.0 Developer preview.

import tornado.web
import tornado.httpserver
import tornado.options
import tornado.ioloop
import tornado.websocket
import tornado.httpclient
from tornado import gen
import os.path
from tornado.options import define, options, parse_command_line
import time

#from couchbase.bucket import Bucket
from twisted.internet import reactor
from txcouchbase.bucket import Bucket

from couchbase.n1ql import N1QLQuery, N1QLError
from pprint import pprint

server = "x.x.x.x"
bucketname = "zips"

Connection = "couchbase://" + server + "/" + bucketname

bkt = Bucket(Connection)

class IndexHandler(tornado.web.RequestHandler):
    @tornado.web.asynchronous   
    def get(self):
        print "entered"
        query = "SELECT * FROM `zips` where pincode= '632014'"
        q = N1QLQuery(query)
        #self.bkt = bkt
        t0 = time.time()
        res = bkt.n1qlQueryAll(q)
        res.addCallback(self.on_ok)
        reactor.run()
        t1 = time.time()
        print t1-t0
        self.write("Hello World")

    def on_ok(self,response):
        print "LOl"
        for each in res:
            print each
        reactor.stop()
        self.finish()

handlers = [
    (r'/',IndexHandler),
]

if __name__ == "__main__":
    parse_command_line()
    # template path should be given here only unlike handlers
    app = tornado.web.Application(handlers, template_path=os.path.join(os.path.dirname(__file__), "templates"),
                                  static_path=os.path.join(os.path.dirname(__file__), "static"), cookie_secret="61oETzKXQAGaYdkL5gEmGeJJFuYh7EQnp2XdTP1o/Vo=", debug=True)
    http_server = tornado.httpserver.HTTPServer(app)
    http_server.listen(8888, address='0.0.0.0')
    tornado.ioloop.IOLoop.instance().start()

After I run this, for some reason the callback function is never called. I could not find any proper documentation for this, and had to go through the source code to write this. I'm still confused as I'm new to asynchronous programming. Can someone please tell me where I'm going wrong and if there is a better way of doing this?


回答1:


In asynchronous programming, you only want to start an event loop (like IOLoop.start() or reactor.run()) once, at the top of your program. You're calling IOLoop.start(), so instead of calling reactor.run() you want to tell Twisted to use the Tornado IOLoop as its reactor. Before the import of reactor, do

import tornado.platform.twisted
tornado.platform.twisted.install()
from twisted.internet import reactor

See http://www.tornadoweb.org/en/stable/twisted.html#twisted-on-tornado for more.

Once you've done this, you can call twisted libraries without having to start and stop the reactor.



来源:https://stackoverflow.com/questions/31437022/twisted-api-for-couchbase-not-working-with-python-tornado

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!