如何在单独的数据访问层中正确使用Motor(How use properly Motor in separate data access layer)

编程入门 行业动态 更新时间:2024-10-27 22:30:13
如何在单独的数据访问层中正确使用Motor(How use properly Motor in separate data access layer)

我开始使用龙卷风和电机的一个小项目,如果我想要一个非bloking访问,我感到有些困惑,如何我必须处理访问数据层

通常我将我的项目与这个结构分开

root_project -logic -data --UsersDao -handlers --Users -main.py

但我不知道我是否做了这样的事情,连接将是非阻塞的

@gen.coroutine @tornado.web.asynchronous def get(self, id): users = self.settings["User"] result = yield from users.get(id) self.write(json_encode(result)) self.finish()

'users'是我的UsersDao对象,看起来像

class UsersDao(object): .... def get(self, user, callback=None): try: user = yield self._db["users"].find_one({'_id': user}) ...create user object return user except ValueError: pass except OperationFailure: pass except Exception: raise

I started a little project using tornado and motor, I feel some confused respect how i have to handle the access data layer if i want to have a non-bloking access

usally i separate my project with this structure

root_project -logic -data --UsersDao -handlers --Users -main.py

but i don't know if i do something like this the connection would be non-blocking

@gen.coroutine @tornado.web.asynchronous def get(self, id): users = self.settings["User"] result = yield from users.get(id) self.write(json_encode(result)) self.finish()

'users' it's my UsersDao object and looks like

class UsersDao(object): .... def get(self, user, callback=None): try: user = yield self._db["users"].find_one({'_id': user}) ...create user object return user except ValueError: pass except OperationFailure: pass except Exception: raise

最满意答案

通常,无论何时使用yield ,您都会执行异步/非阻塞操作。 所以在这种情况下,你发布的代码看起来是正确的,除了在@gen.coroutine上缺少@gen.coroutine装饰器(每当你使用yield for asynchronous stuff时,你需要这个装饰器,你需要在你调用它时使用yield )。

In general, whenever you use yield, you're doing something asynchronous/non-blocking. So in this case the code you've posted looks correct except for the missing @gen.coroutine decorator on UsersDao.get (whenever you use yield for asynchronous stuff, you need this decorator, and you need to use yield any time you call it).

更多推荐

本文发布于:2023-08-06 21:32:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1455645.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:层中   正确   数据   如何在   Motor

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!