def __init__(self): tornado.web.Application.__init__(self, url_patterns, **settings) self._redis = _db.redis_client self._motor = get_db(CONFIG.MONGO.DATABASE, client='motor') connect(CONFIG.MONGO.DATABASE, host=CONFIG.MONGO.HOST, port=CONFIG.MONGO.PORT, io_loop=tornado.ioloop.IOLoop.current()) # motorengine
def __init__(self): tornado.web.Application.__init__(self, url_patterns, **settings) self._redis = _db.redis_client self._motor = get_mongodb(CONFIG.MONGO.DATABASE, client='motor') connect(CONFIG.MONGO.DATABASE, host=CONFIG.MONGO.HOST, port=CONFIG.MONGO.PORT, io_loop=tornado.ioloop.IOLoop.current()) # motorengine
def main(): app = Application() app.listen(8888) # Connect to MongoDB server io_loop = tornado.ioloop.IOLoop.instance() connect(DB['name'], host=DB['host'], port=DB['port'], io_loop=io_loop) tornado.ioloop.IOLoop.current().start()
def run(settings_module): """ Run the app using specified settings """ settings = importlib.import_module(settings_module) app = tornado.web.Application(routes.urls, **settings.TORNADO) app.listen(settings.TORNADO['port'], '127.0.0.1') io_loop = tornado.ioloop.IOLoop.instance() connect(settings.MONGO['name'], io_loop=io_loop, **settings.MONGO['options']) io_loop.start()
#!/usr/bin/env python # encoding: utf-8 import tornado.ioloop from motorengine.connection import connect from motorengine import Document, StringField io_loop = tornado.ioloop.IOLoop.instance() connect("tornado_test", host="localhost", port=27017, io_loop=io_loop) class User(Document): __collection__ = "users" # optional. if no collection is specified, class name is used. first_name = StringField(required=True) last_name = StringField(required=True) @property def full_name(self): return "%s, %s" % (self.last_name, self.first_name)
def setUp(self): self.io_loop = IOLoop.current() connect(CONFIG.MONGO.DATABASE, host=CONFIG.MONGO.HOST, port=CONFIG.MONGO.PORT, io_loop=self.io_loop) # connect mongoengine
post = copy.deepcopy(save_post) # note use deepcopy post['slug'] = post['slug'] + str(i) post_obj = Post(**post) post_list.append(post_obj) yield Post.objects.bulk_insert(post_list) post_obj_list = yield Post.objects.find_all() assert n == len(post_obj_list) nums = yield Post.objects.delete() assert nums == n @coroutine def create_posts(): user = yield User.objects.create(**save_user) post_list = [] n = 35 for i in range(n): post = copy.deepcopy(save_post) # note use deepcopy post['slug'] = post['slug'] + str(i) post['author'] = user post_obj = Post(**post) post_list.append(post_obj) yield Post.objects.bulk_insert(post_list) if __name__ == '__main__': connect(CONFIG.MONGO.DATABASE, host=CONFIG.MONGO.HOST, port=CONFIG.MONGO.PORT, io_loop=IOLoop.current()) # connect mongoengine IOLoop.current().run_sync(create_posts)
from tornado.httpserver import HTTPServer from tornado.options import parse_command_line from tornado.ioloop import IOLoop from motorengine.connection import connect import settings from app import app http_server = HTTPServer(app, xheaders=True) http_server.listen(settings.port) io_loop = IOLoop.current() connect( settings.database['name'], host=settings.database['host'], port=settings.database['port'], io_loop=io_loop ) # init database in debug if settings.debug: from init_site import init_db io_loop.add_timeout(5, init_db) if __name__ == "__main__": logger = logging.getLogger("tornado.application") parse_command_line() logger.info('Server running on http://0.0.0.0:%d' % (settings.port)) io_loop.start()
# coding=utf8 import logging from tornado.httpserver import HTTPServer from tornado.options import parse_command_line from tornado.ioloop import IOLoop from motorengine.connection import connect import settings from app import app http_server = HTTPServer(app, xheaders=True) http_server.listen(settings.port) io_loop = IOLoop.current() connect(settings.database['name'], host=settings.database['host'], port=settings.database['port'], io_loop=io_loop) # init database in debug if settings.debug: from init_site import init_db io_loop.add_timeout(5, init_db) if __name__ == "__main__": logger = logging.getLogger("tornado.application") parse_command_line() logger.info('Server running on http://0.0.0.0:%d' % (settings.port)) io_loop.start()
#!/usr/bin/python # -*- coding: utf-8 -*- import os import tornado.ioloop import pyrestful.rest import settings from motorengine.connection import connect from libs.handler_finder import HandlerFinder ROOT_PATH = os.path.dirname(__file__) if __name__ == '__main__': finder = HandlerFinder(root_path=ROOT_PATH) finder.find(settings.INSTALLED_APPS) app = pyrestful.rest.RestService(finder.handlers, **settings.app_settings) app.listen(settings.SERVER_PORT) io_loop = tornado.ioloop.IOLoop.instance() connect( settings.MONGODB_CONNECT['database'], host=settings.MONGODB_CONNECT['host'], port=settings.MONGODB_CONNECT['port'], io_loop=io_loop ) print 'Web Server starting......' io_loop.start()