如何在龙卷风中的BaseIOStream类对象中设置max_write_buffer_size?

时间:2017-01-24 09:15:11

标签: tornado

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

2 个答案:

答案 0 :(得分:0)

source code,有人可以发现max_write_buffer_size不是私有变量。因此,您可以从BaseIOStream对象或从SSLIOStream继承的类IOStreamBaseIOStream的任何对象访问它。

例如:

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()

答案 1 :(得分:0)

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

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()