问题描述
我试图运行与Couchbase 4.0开发preVIEW龙卷风服务器。
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()
我跑在此之后,由于某些原因,回调函数永远不会被调用。我找不到这方面的任何适当的文件,并且要经过源$ C $ C写这个。我仍然感到困惑,因为我是新来的异步编程。是否有人可以告诉我,我要去哪里错了,如果有这样做的更好的办法?
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?
推荐答案
在异步编程,你只需要启动一个事件循环(如 IOLoop.start()
或 reactor.run()
)一次,在程序的顶部。你打电话 IOLoop.start()
这样的,而不是调用 reactor.run()
你想告诉扭曲使用旋风IOLoop作为其反应器中。 反应堆
进口之前,执行
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
请参阅了解。
一旦你做到了这一点,你可以叫扭曲库,而无需启动和停止反应器。
Once you've done this, you can call twisted libraries without having to start and stop the reactor.
这篇关于为Couchbase扭曲的API不使用Python龙卷风工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!