Answer the question
In order to leave comments, you need to log in
How and where to transfer database connections to Tornado?
Hello.
I would like to consult about the correct connection to the database.
I am using the Tornado framework, the base is MongoDB.
The structure of my application:
- server.py / Create Application, start the server
- handlers / Controllers
----main.py
----users.py
- models / Models
---- users.py
---- stats.py
---- db.py
Previously, I opened a connection in db.py and imported it into all models.
But recently, a more experienced colleague said that it is better to create and pass a connection to the Application class, that is, 1 connection for the entire application?
Can you tell me if this is the right tactic?
Answer the question
In order to leave comments, you need to log in
The book Introduction to Tornado has a section dedicated to working specifically with MongoDB.
The connection is made like this:
class Application(tornado.web.Application):
def __init__(self):
handlers = [(r"/(\w+)", WordHandler)]
conn = pymongo.Connection("localhost", 27017)
self.db = conn["example"] tornado.web.Application.__init__(self, handlers, debug=True)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question