【问题标题】:how to set max_write_buffer_size in BaseIOStream class object in tornado?如何在龙卷风的 BaseIOStream 类对象中设置 max_write_buffer_size?
【发布时间】:2017-01-24 09:15:10
【问题描述】:

在 tornado 网络服务器代码中,SSLIOStream 继承自 IOStream 并继承 BaseIOStream 类。 BaseIOStream 具有 max_write_buffer_size 从我们启动龙卷风服务器的位置。我没有找到要设置的环境变量的任何用法,它必须作为构造函数参数传递。

【问题讨论】:

    标签: tornado


    【解决方案1】:

    source code 有人可以观察到max_write_buffer_size 不是私有变量。因此,您可以从BaseIOStream 对象或从BaseIOStream 继承的SSLIOStreamIOStream 类的任何对象访问它。

    例如:

    import socket
    import tornado.ioloop
    import tornado.iostream
    import tornado.options
    
    # def connect_baseiostream():
    #    sock = socket.socket()
    #    stream = tornado.iostream.BaseIOStream(sock)
    #    stream.max_write_buffer_size = 10000 # Set max_write_buffer_size
    
    def connect_iostream():
        sock = socket.socket()
        stream = tornado.iostream.IOStream(sock)
        stream.max_write_buffer_size = 10000 # Set max_write_buffer_size
        stream.connect(host, port) # Defined host and port
    
    def main():
        tornado.options.parse_command_line()
        tornado.ioloop.IOLoop.instance().add_callback(connect_iostream)
        tornado.ioloop.IOLoop.instance().start()
    
    if __name__ == "__main__":
        main()
    

    【讨论】:

      【解决方案2】:

      这个参数没有暴露在 HTTP 服务器中,因为它的使用是有限的,并且可能默认值是正确的(无限制)。当然,您可以为此创建一个问题并在github 上讨论它。另一种解决方案是使用自己的TCPServer's handle_connection 实现子类tornado.httpserver.HTTPServer(请记住它是私有的_ 方法)。

      from tornado.httpserver import HTTPServer
      
      max_write_buffer_size = 65535
      
      class CustomHTTPServer(HTTPServer):
      
          def _handle_connection(self, connection, address):
          # https://github.com/tornadoweb/tornado/blob/master/tornado/tcpserver.py#L257
                 if self.ssl_options is not None:
                     assert ssl, "Python 2.6+ and OpenSSL required for SSL"
                     try:
                         connection = ssl_wrap_socket(connection,
                                                      self.ssl_options,
                                                      server_side=True,
                                                      do_handshake_on_connect=False)
                     except ssl.SSLError as err:
                         if err.args[0] == ssl.SSL_ERROR_EOF:
                             return connection.close()
                         else:
                             raise
                     except socket.error as err:
                         if errno_from_exception(err) in (errno.ECONNABORTED, errno.EINVAL):
                             return connection.close()
                         else:
                             raise
                 try:
                     if self.ssl_options is not None:
                         stream = SSLIOStream(connection, io_loop=self.io_loop,
                                              max_buffer_size=self.max_buffer_size,
                                              read_chunk_size=self.read_chunk_size,
                                              max_write_buffer_size=max_write_buffer_size)
                     else:
                         stream = IOStream(connection, io_loop=self.io_loop,
                                           max_buffer_size=self.max_buffer_size,
                                           read_chunk_size=self.read_chunk_size,
                                           max_write_buffer_size=max_write_buffer_size)
      
                     future = self.handle_stream(stream, address)
                     if future is not None:
                         self.io_loop.add_future(gen.convert_yielded(future),
                                                 lambda f: f.result())
                 except Exception:
                     app_log.error("Error in connection callback", exc_info=True)
      

      并使用自定义服务器:

          application = web.Application([
              (r"/", MainPageHandler),
          ])
          http_server = CustomHTTPServer(application)
          http_server.listen(8080)
          ioloop.IOLoop.current().start()
      

      【讨论】:

        猜你喜欢
        • 2017-10-08
        • 1970-01-01
        • 1970-01-01
        • 2011-07-24
        • 2017-08-02
        • 2017-09-15
        • 2017-06-16
        • 2018-12-17
        • 1970-01-01
        相关资源
        最近更新 更多