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
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).