0

我试图更改我的代码以使用MongoTor使其异步

这是我的简单代码:

class BaseHandler(tornado.web.RequestHandler):
    @property 
    def db(self):
        if not hasattr(self,"_db"):
            _db = Database.connect('localhost:27017', 'essog')
            return _db
    @property
    def fs(self):
        if not hasattr(BaseHandler,"_fs"):
            _fs = gridfs.GridFS(self.db)
            return _fs


class LoginHandler(BaseHandler):
    @tornado.web.asynchronous
    @tornado.gen.engine
    def post(self):
        email = self.get_argument("email")
        password = self.get_argument("pass1")
        try:
            search = yield tornado.gen.Task(self.db.users.find, {"prs.mail":email})
            ....

我收到了这个错误:

Traceback (most recent call last):
File "C:\Python27\lib\site-packages\tornado-2.4.post1-py2.7.egg\tornado\web.py", line 1043, in _stack_context_handle_exception
raise_exc_info((type, value, traceback))
File "C:\Python27\lib\site-packages\tornado-2.4.post1-py2.7.egg\tornado\web.py", line 1162, in wrapper
return method(self, *args, **kwargs)
File "C:\Python27\lib\site-packages\tornado-2.4.post1-py2.7.egg\tornado\gen.py", line 122, in wrapper
runner.run()
File "C:\Python27\lib\site-packages\tornado-2.4.post1-py2.7.egg\tornado\gen.py", line 365, in run
yielded = self.gen.send(next)
File "G:\Mon projet\essog\handlers.py", line 92, in post
search = yield tornado.gen.Task(self.db.users.find, {"prs.mail":email})
File "G:\Mon projet\essog\handlers.py", line 62, in db
_db = Database.connect('localhost:27017', 'essog')
File "build\bdist.win-amd64\egg\mongotor\database.py", line 131, in connect
database.init(addresses, dbname, read_preference, **kwargs)
File "build\bdist.win-amd64\egg\mongotor\database.py", line 62, in init
ioloop_is_running = IOLoop.instance().running()
 AttributeError: 'SelectIOLoop' object has no attribute 'running'
 ERROR:tornado.access:500 POST /login (::1) 3.00ms

这是另一个问题,在这种情况下我该怎么做distinct

这是在阻塞模式下起作用的:

search = self.db.users.find({"prs.mail":email}).distinct("prs.mail")[0]

更新:

似乎没有运行 Tornado 时会发生此错误!仅在控制台中使用模块时引发相同的错误。

test = Database.connect("localhost:27017", "essog") ---------------------------------- ----------------------------------------- AttributeError Traceback(最近一次调用最后一次)在() ----> 1 test = Database.connect("localhost:27017", "essog")

C:\Python27\lib\site-packages\mongotor-0.0.10-py2.7.egg\mongotor\database.pyc in connect(cls,addresses,dbname,read_preference,**kwargs) 131 132 database = Database() --> 133 database.init(addresses, dbname, read_preference, **kwargs) 134 135 返回数据库

C:\Python27\lib\site-packages\mongotor-0.0.10-py2.7.egg\mongotor\database.pyc in init(self,addresses,dbname,read_preference,**kwargs) 60 self._nodes.append(节点) 61 ---> 62 ioloop_is_running = IOLoop.instance().running() 63 self._config_nodes(callback=partial(self._on_config_node, ioloop_is_running)) 64

AttributeError:“SelectIOLoop”对象没有“正在运行”的属性

4

1 回答 1

0

我得到了答案:

  1. 现在,Tornado 2.4post1 已经删除了 running(),所以要使用实际的 MongoTor,请使用 tornado 2.4

编辑:tornado 和 mongotor 现在使用最新版本效果更好。

  1. 使用不同的

    distinc_uuids = yield gen.Task(db.collection_test.find({"param":'shouldbeparam1'}).distinct, 'uuid')
    
于 2012-11-11T11:57:54.897 回答