J
J
Jeket2014-11-28 00:15:31
Python
Jeket, 2014-11-28 00:15:31

Where can I find a working tornado motor sample?

Experimenting with tornado as an asynchronous mongo driver trying to use motor. My code:

class Application(tornado.web.Application):
  def __init__(self):
    db_name = config['database']['name']
    self.db = motor.MotorClient(config['database']['host'], config['database']['port'])[db_name]

    settings = dict(
      template_path=os.path.join(os.path.dirname(__file__), "templates"),
      static_path=os.path.join(os.path.dirname(__file__), "static"),
      debug=True,
      autoescape=None
      )
    tornado.web.Application.__init__(self, url_patterns, **settings)

The handler itself (I want to write a test entry in the mongo when the page is called):
class MainHandler(tornado.web.RequestHandler):
  def get(self):
    doc={'who': 'data who'}
    self.application.db.insert(doc, callback=self.some_function)

But I get an error:
TypeError: MotorCollection object is not callable. If you meant to call the 'insert' method on a MotorCollection object it is failing because no such method exists.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
J
Jeket, 2014-11-29
@Jeket

Everything turned out to be simple - you need to insert values ​​into the collection, but it was inserted into the
self.application.db database. chat_messages .insert(doc, callback=self.some_function)

S
Singularity, 2014-11-28
@Singularity

class MainHandler(tornado.web.RequestHandler):
@tornado.web.asynchronous
def get(self):
doc={'who': 'data who'}
self.application.db.insert(doc, callback=self.some_function)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question