Created
November 4, 2014 08:36
-
-
Save mehmetkose/a11b11679c37b46a66e9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import tornado.ioloop | |
import tornado.web | |
import logging | |
import motor | |
client = motor.MotorClient('mongodb://localhost:27017') | |
db = client.test_database | |
class MainHandler(tornado.web.RequestHandler): | |
@tornado.web.asynchronous | |
def get(self): | |
db = self.settings['db'] | |
document = {'key': 'value'} | |
self.write("hellno world") | |
db.test_collection.insert(document, callback=self.my_callback) | |
def my_callback(self, result, error): | |
if error: | |
logging.error("wtf") | |
if result: | |
repr(result) | |
self.write("yea") | |
tornado.ioloop.IOLoop.instance().stop() | |
application = tornado.web.Application([ | |
(r"/", MainHandler), | |
], db=db) | |
if __name__ == "__main__": | |
application.listen(8888) | |
tornado.ioloop.IOLoop.instance().start() |
Thanks Jesse, I updated the code.
import tornado.ioloop
import tornado.web
import logging
import motor
import time
client = motor.MotorClient('mongodb://localhost:27017')
db = client.test_database
class MainHandler(tornado.web.RequestHandler):
@tornado.web.asynchronous
def get(self):
db = self.settings['db']
document = {'key': time.time()}
self.write("hell-no world")
db.test_collection.insert(document, callback=self.my_callback)
def my_callback(self, result, error):
if error:
logging.error("wtf")
if result:
self.write(repr(result))
self.finish()
application = tornado.web.Application([
(r"/", MainHandler),
], db=db)
if __name__ == "__main__":
application.listen(8888)
tornado.ioloop.IOLoop.instance().start()
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Remove the call to IOLoop.instance().stop(), since that terminates your application. When you want to quit the application just to Ctrl-C on your keyboard from the command line.
Instead, at the end of my_callback, call self.finish().
The rest of your code looks correct to me.