Esempio n. 1
0
def run_server(
    app: tornado.web.Application,
    service: AddressBookService,
    config: Dict,
    port: int,
    logger: logging.Logger,
    debug: bool,
):
    name = config['service']['name']

    # Install async IO event loop instead of Tornado's IO loop for standard
    # async/await code and Tornado to work in same even loop.
    tasyncio.AsyncIOMainLoop().install()

    # Register loop.stop() as signal handler
    # In 3.6, shutting down gracefully is a lot simpler:
    #
    # try:
    #     loop.run_forever()
    # finally:
    #     loop.run_until_complete(loop.shutdown_asyncgens())
    #     loop.close()
    loop = asyncio.get_event_loop()
    for sig in (signal.SIGINT, signal.SIGTERM):
        loop.add_signal_handler(sig, loop.stop)

    # Start auth, caller's start_up and server
    service.start()

    # Start http server
    http_server_args = {'decompress_request': True}

    http_server = app.listen(port, '', **http_server_args)
    msg = 'Starting {} on port {} ...'.format(name, port)
    logger.info(msg)

    # Start event loop
    # asyncio equivalent of tornado.ioloop.IOLoop.current().start()
    loop.run_forever()

    # Begin shutdown after loop.stop() upon receiving signal
    msg = 'Shutting down {}...'.format(name)
    logger.info(msg)

    http_server.stop()
    # Run unfinished tasks and stop event loop
    loop.run_until_complete(asyncio.gather(*asyncio.Task.all_tasks()))
    service.stop()
    loop.close()

    # Service stopped
    msg = 'Stopped {}.'.format(name)
    logger.info(msg)
Esempio n. 2
0
def run_server() -> None:
    define("port", default=8888, help="Run on the given port", type=int)

    asyncio.AsyncIOMainLoop().install()
    io_loop = IOLoop.current()
    enable_pretty_logging()

    app = DeCvApplication()
    max_buffer_size = 2 * 1024**3  # 2GB
    http_server = HTTPServer(
        app,
        max_buffer_size=max_buffer_size,
    )
    http_server.listen(options.port)
    io_loop.start()
 def get_new_ioloop(self):
     return tasyncio.AsyncIOMainLoop()
import asyncio
import tornado.gen as gen
import tornado.httpserver as httpserver
import tornado.platform.asyncio as tornado_asyncio
import tornado.web as web
import logging


class IndexHandler(web.RequestHandler):
    async def get(self):
        self.finish({"hello": "world"})


class App(web.Application):
    def __init__(self):
        settings = {'debug': False}
        super(App, self).__init__(handlers=[
            (r'/', IndexHandler),
        ],
                                  **settings)


if __name__ == '__main__':
    logging.getLogger('tornado.access').disabled = True
    asyncio.set_event_loop_policy(uvloop.EventLoopPolicy())
    tornado_asyncio.AsyncIOMainLoop().install()
    app = App()
    server = httpserver.HTTPServer(app, xheaders=True)
    server.listen(8006)
    asyncio.get_event_loop().run_forever()
Esempio n. 5
0
from tornado.options import define, options
from tornroutes import route, generic_route
from wechat.plugin_manager import PluginManager
from util.httpapi import HttpApi
from tornado_jinja2 import Jinja2Loader
from wechat.util.wechat_util import wechatUtil

define('port', default=7770, help='Run server on a specific port', type=int)
define('host', default='localhost', help='Run server on a specific host')
define('url', default=None, help='Url to show in HTML')
define('config', default='./config.yaml', help="config file's full path")
tornado.options.parse_command_line()

from tornado.platform import asyncio as todoasyncio

todoasyncio.AsyncIOMainLoop().install()

if not options.url:
    options.url = 'http://{}:{}'.format(options.host, options.port)

settings = {
    'base_url': options.url,
    'config_filename': options.config,
    'compress_response': True,
    'xsrf_cookies': False,
    'static_path': 'static',
    'template_path': 'templates',
    'session': {
        'driver': 'redis',
        'driver_settings': {
            'host': 'localhost',