3种启动tornado的方式

Posted renfanzi

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了3种启动tornado的方式相关的知识,希望对你有一定的参考价值。

 

r"""A non-blocking, single-threaded HTTP server.
    
    翻译: 一个非阻塞的单线程HTTP服务器
    
    A server is defined by a subclass of `.HTTPServerConnectionDelegate`,
    or, for backwards compatibility, a callback that takes an
    `.HTTPServerRequest` as an argument. The delegate is usually a
    `tornado.web.Application`.
    
    翻译:服务器由".HTTPServerConnectionDelegate"的子类定义,或者,对于向后兼容,一个回调".HTTPServerRequest"作为一个参数。委托通常是 "tornado.web.Application"。
    
    `HTTPServer` supports keep-alive connections by default
    (automatically for HTTP/1.1, or for HTTP/1.0 when the client
    requests ``Connection: keep-alive``).

    翻译:默认情况下,HTTPServer支持keep-alive连接(自动为HTTP/1.1,或HTTP/1.0,当客户端请求的连接:维生”)。

    If ``xheaders`` is ``True``, we support the
    ``X-Real-Ip``/``X-Forwarded-For`` and
    ``X-Scheme``/``X-Forwarded-Proto`` headers, which override the
    remote IP and URI scheme/protocol for all requests.  These headers
    are useful when running Tornado behind a reverse proxy or load
    balancer.  The ``protocol`` argument can also be set to ``https``
    if Tornado is run behind an SSL-decoding proxy that does not set one of
    the supported ``xheaders``.

    翻译:
    如果"xheader""True",我们支持``X-Real-Ip``/``X-Forwarded-For````X-Scheme``/``X-Forwarded-Proto``的头,它覆盖了
    
    所有请求的远程IP和URI模式/协议。这些头当在反向代理或负载下运行Tornado时是有用的均衡器。"protocol协议"的参数也可以设置为"https"。

    如果Tornado是在一个SSL-decoding的代理后面运行的,它不会设置一个支持的“xheaders””。

    By default, when parsing the ``X-Forwarded-For`` header, Tornado will
    select the last (i.e., the closest) address on the list of hosts as the
    remote host IP address.  To select the next server in the chain, a list of
    trusted downstream hosts may be passed as the ``trusted_downstream``
    argument.  These hosts will be skipped when parsing the ``X-Forwarded-For``
    header.

    翻译:
    默认情况下,当解析``X-Forwarded-For``的header时,Tornado将会
    选择最后一个(例如最接近的)在主机列表上的地址
    远程主机IP地址。要选择链中的下一个服务器,一个列表
    受信任的下游主机可以作为``trusted_downstream``“托管”
    论点。在解析``X-Forwarded-For``时,这些主机将被跳过头。

    To make this server serve SSL traffic, send the ``ssl_options`` keyword
    argument with an `ssl.SSLContext` object. For compatibility with older
    versions of Python ``ssl_options`` may also be a dictionary of keyword
    arguments for the `ssl.wrap_socket` method.::

    翻译:
    要使这个服务器服务于SSL流量,请发送“ssloptions”关键字
    用“ssl”进行论证。SSLContext”对象。为了与旧版本的兼容性
    Python“ssloptions”的版本也可能是关键字的字典
    关于“ssl”的争论。wrap_socket”方法。

    针对https的做法

       ssl_ctx = ssl.create_default_context(ssl.Purpose.CLIENT_AUTH)
       ssl_ctx.load_cert_chain(os.path.join(data_dir, "mydomain.crt"),
                               os.path.join(data_dir, "mydomain.key"))
       HTTPServer(applicaton, ssl_options=ssl_ctx)




      from tornado.httpserver import HTTPServer
      from tornado.ioloop import IOLoop


    `HTTPServer` initialization follows one of three patterns (the
    initialization methods are defined on `tornado.tcpserver.TCPServer`):

    1. `~tornado.tcpserver.TCPServer.listen`: simple single-process:: 单进程

            server = HTTPServer(app)
            server.listen(8888)
            IOLoop.current().start()

       In many cases, `tornado.web.Application.listen` can be used to avoid
       the need to explicitly create the `HTTPServer`.

    2. `~tornado.tcpserver.TCPServer.bind`/`~tornado.tcpserver.TCPServer.start`:
       simple multi-process::

            server = HTTPServer(app)
            server.bind(8888)
            server.start(0)  # Forks multiple sub-processes  // Forks 多个单进程
            IOLoop.current().start()

       When using this interface, an `.IOLoop` must *not* be passed
       to the `HTTPServer` constructor.  `~.TCPServer.start` will always start
       the server on the default singleton `.IOLoop`.

    3. `~tornado.tcpserver.TCPServer.add_sockets`: advanced multi-process:: # 高级的多进程, 这个好处就是同脚本启动多个不同端口服务, 2则不可以

            sockets = tornado.netutil.bind_sockets(8888)
            tornado.process.fork_processes(0) # 10
            server = HTTPServer(app)
            server.add_sockets(sockets)
            IOLoop.current().start()

       The `~.TCPServer.add_sockets` interface is more complicated,
       but it can be used with `tornado.process.fork_processes` to
       give you more flexibility in when the fork happens.
       `~.TCPServer.add_sockets` can also be used in single-process
       servers if you want to create your listening sockets in some
       way other than `tornado.netutil.bind_sockets`.

    .. versionchanged:: 4.0
       Added ``decompress_request``, ``chunk_size``, ``max_header_size``,
       ``idle_connection_timeout``, ``body_timeout``, ``max_body_size``
       arguments.  Added support for `.HTTPServerConnectionDelegate`
       instances as ``request_callback``.

    .. versionchanged:: 4.1
       `.HTTPServerConnectionDelegate.start_request` is now called with
       two arguments ``(server_conn, request_conn)`` (in accordance with the
       documentation) instead of one ``(request_conn)``.

    .. versionchanged:: 4.2
       `HTTPServer` is now a subclass of `tornado.util.Configurable`.

    .. versionchanged:: 4.5
       Added the ``trusted_downstream`` argument.
    """

 

 

tornado https 该怎么写

    翻译:
    要使这个服务器服务于SSL流量,请发送“ssloptions”关键字
    用“ssl”进行论证。SSLContext”对象。为了与旧版本的兼容性
    Python“ssloptions”的版本也可能是关键字的字典
    关于“ssl”的争论。wrap_socket”方法。

    针对https的做法

       ssl_ctx = ssl.create_default_context(ssl.Purpose.CLIENT_AUTH)
       ssl_ctx.load_cert_chain(os.path.join(data_dir, "mydomain.crt"),
                               os.path.join(data_dir, "mydomain.key"))
       HTTPServer(applicaton, ssl_options=ssl_ctx)

方法二、
 server = HTTPServer(application, max_buffer_size=504857600, ssl_options={"certfile":"/etc/letsencrypt/live/技术分享图片zkey.cc/fullchain.pem", "keyfile":"/etc/letsencrypt/live/技术分享图片zkey.cc/privkey.pem"}) 

 

 

第一种启动方式

      from tornado.httpserver import HTTPServer
      from tornado.ioloop import IOLoop


    `HTTPServer` initialization follows one of three patterns (the
    initialization methods are defined on `tornado.tcpserver.TCPServer`):

    1. `~tornado.tcpserver.TCPServer.listen`: simple single-process:: 单进程

            server = HTTPServer(app)
            server.listen(8888)
            IOLoop.current().start()

       In many cases, `tornado.web.Application.listen` can be used to avoid
       the need to explicitly create the `HTTPServer`.

 

 

第二种启动方式

      from tornado.httpserver import HTTPServer
      from tornado.ioloop import IOLoop
2. `~tornado.tcpserver.TCPServer.bind`/`~tornado.tcpserver.TCPServer.start`:
       simple multi-process::

            server = HTTPServer(app)
            server.bind(8888)
            server.start(0)  # Forks multiple sub-processes  // Forks 多个单进程
            IOLoop.current().start()

       When using this interface, an `.IOLoop` must *not* be passed
       to the `HTTPServer` constructor.  `~.TCPServer.start` will always start
       the server on the default singleton `.IOLoop`.

 

第三种启动方式

      from tornado.httpserver import HTTPServer
      from tornado.ioloop import IOLoop
3. `~tornado.tcpserver.TCPServer.add_sockets`: advanced multi-process:: # 高级的多进程, 这个好处就是同脚本启动多个不同端口服务, 2则不可以

            sockets = tornado.netutil.bind_sockets(8888)
            tornado.process.fork_processes(0) # 10
            server = HTTPServer(app)
            server.add_sockets(sockets)
            IOLoop.current().start()

       The `~.TCPServer.add_sockets` interface is more complicated,
       but it can be used with `tornado.process.fork_processes` to
       give you more flexibility in when the fork happens.
       `~.TCPServer.add_sockets` can also be used in single-process
       servers if you want to create your listening sockets in some
       way other than `tornado.netutil.bind_sockets`.

 

以上是关于3种启动tornado的方式的主要内容,如果未能解决你的问题,请参考以下文章

一张图,理顺 Spring Boot应用在启动阶段执行代码的几种方式

一张图帮你记忆,Spring Boot 应用在启动阶段执行代码的几种方式

一张图,理顺 Spring Boot应用在启动阶段执行代码的几种方式

Tornado 高并发源码分析之二---Tornado启动和请求处理流程

tornado 启动WSGI应用(Flask)使用多线程将同步代码变成异步

tornado基本使用一