Tornado 4.2的新功能

2015年5月26日

向后兼容性说明

  • SSLIOStream.connectIOStream.start_tls 现在默认验证证书。

  • 证书验证现在将使用系统CA根证书而不是 certifi 如果可能(即python 2.7.9+或3.4+)。这包括 IOStreamsimple_httpclient ,但不是 curl_httpclient .

  • 默认的SSL配置变得更严格,使用 ssl.create_default_context 在客户端可用时。(在服务器端,鼓励应用程序从 ssl_options 基于dict的API传递 ssl.SSLContext 相反)。

  • 中已弃用的类 tornado.auth 模块, GoogleMixinFacebookMixinFriendFeedMixin 已被删除。

新模块: tornado.lockstornado.queues

这些模块提供用于协调协同程序的类,从 Toro .

要将代码从Toro的队列移植到Tornado 4.2,请导入 QueuePriorityQueueLifoQueuetornado.queues 而不是来自 toro .

使用 Queue 而不是托罗的 JoinableQueue . 在龙卷风中的方法 jointask_done 所有队列都可用,而不是特殊队列 JoinableQueue .

Tornado队列会引发特定于Tornado的异常,而不是重用Python标准库中的异常。因此没有达到标准 queue.Empty 例外 Queue.get_nowait ,接住特别节目 tornado.queues.QueueEmpty 例外,而不是标准 queue.FullQueue.get_nowait ,捕获 tornado.queues.QueueFull .

从Toro的船闸到Tornado 4.2的港口,进口 ConditionEventSemaphoreBoundedSemaphoreLocktornado.locks 而不是来自 toro .

托罗 Semaphore.wait 允许协同程序等待信号灯解锁 没有 获取它。这鼓励了非正统的模式;在龙卷风中,只需使用 acquire .

托罗 Event.wait 提出一个 Timeout 超时后出现异常。在龙卷风中, Event.wait 加薪 tornado.gen.TimeoutError .

托罗 Condition.wait 也提出 Timeout 但是在龙卷风中, Future 返回的 Condition.wait 超时后解析为false::

@gen.coroutine
def await_notification():
    if not (yield condition.wait(timeout=timedelta(seconds=1))):
        print('timed out')
    else:
        print('condition is true')

在锁定和队列方法中,只要Toro接受 deadline 作为关键字参数,Tornado将参数命名为 timeout 相反。

托罗 AsyncResult 不合并为龙卷风,也不合并为龙卷风例外。 NotReadyAlreadySet . 使用A Future 相反。如果您这样编写代码:

from tornado import gen
import toro

result = toro.AsyncResult()

@gen.coroutine
def setter():
    result.set(1)

@gen.coroutine
def getter():
    value = yield result.get()
    print(value)  # Prints "1".

那么 Tornado 当量为:

from tornado import gen
from tornado.concurrent import Future

result = Future()

@gen.coroutine
def setter():
    result.set_result(1)

@gen.coroutine
def getter():
    value = yield result
    print(value)  # Prints "1".

tornado.autoreload

  • 改进了与Windows的兼容性。

  • 修复了在重新加载检查期间导入模块时python 3中的错误。

tornado.concurrent

tornado.curl_httpclient

  • 修复了一个错误,如果在队列中的某些位置发生异常,将导致客户端停止处理请求。

tornado.escape

  • xhtml_escape 现在支持十六进制格式的数字字符引用 ( 

tornado.gen

  • WaitIterator 不再使用弱引用,它修复了几个与垃圾收集相关的错误。

  • tornado.gen.Multitornado.gen.multi_future (在协程中生成列表或dict时使用)现在记录第一个(如果不止一个)之后的任何异常 Future 失败(以前当 Future 被垃圾收集,但这更可靠)。两者都有一个新的关键字参数 quiet_exceptions 若要禁止某些异常类型的日志记录;若要使用此参数,必须调用 Multimulti_future 直接而不是简单地生成一个列表。

  • multi_future 现在,如果给多个副本相同 Future .

  • 在Python3上,在协程中捕获异常不再导致通过 Exception.__context__ .

tornado.httpclient

tornado.httpserver

tornado.httputil

tornado.ioloop

  • 这个 IOLoop 构造函数现在有一个 make_current 关键字参数来控制 IOLoop 变成 IOLoop.current() .

  • 第三方实施 IOLoop 应该接受 **kwargs 在他们 IOLoop.initialize 方法并将它们传递给超类实现。

  • PeriodicCallback 当时钟大幅向前跳时,效率会更高。

tornado.iostream

  • SSLIOStream.connectIOStream.start_tls 现在默认验证证书。

  • 新方法 SSLIOStream.wait_for_handshake 允许服务器端应用程序等待握手完成,以便验证客户端证书或使用NPN/ALPN。

  • 这个 Future 返回的 SSLIOStream.connect 现在在握手完成后解决,而不是在建立TCP连接后立即解决。

  • 减少了SSL错误的日志记录。

  • BaseIOStream.read_until_close 现在,当 streaming_callback 给予但 callback 无(即当它返回 Future

tornado.locale

tornado.log

tornado.process

tornado.simple_httpclient

  • 通过重用单个 ssl.SSLContext .

  • 新建构造函数参数 max_body_size 控制客户端愿意接受的最大响应大小。它可能比 max_buffer_size 如果 streaming_callback 使用。

tornado.tcpserver

tornado.util

tornado.web

  • 对cookie签名的密钥版本控制支持。 cookie_secret 应用程序设置现在可以包含以版本为键的有效键的dict。然后必须通过指定当前签名密钥 key_version 设置。

  • 解析 If-None-Match header现在跟随RFC并支持弱验证器。

  • 经过 secure=Falsehttponly=FalseRequestHandler.set_cookie 现在按预期工作(以前只考虑参数的存在并忽略其值)。

  • RequestHandler.get_arguments 现在需要的是 strip 参数的类型为bool。这有助于防止由于单数和复数方法之间的接口稍有不同而导致的错误。

  • 在中引发的错误 _handle_request_exception 现在记录更可靠。

  • RequestHandler.redirect 现在,当从路径以两个斜杠开头的处理程序调用时,可以正常工作。

  • 传递包含 % 字符到 tornado.web.HTTPError 不再导致错误消息中断。

tornado.websocket

  • 这个 on_close 方法将不再被多次调用。

  • 当另一方关闭连接时,我们现在回送接收到的关闭代码,而不是发送一个空的关闭帧。