N
N
Nodar2014-08-31 12:13:43
MongoDB
Nodar, 2014-08-31 12:13:43

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

2 answer(s)
Y
Yuri Belyakov, 2014-09-01
@theBookoff

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)

the truth is there in the examples the entire application is placed in one main.py.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question