ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、视频、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
### Navigation - [index](# "General Index") - [modules](# "Python Module Index") | - [next](# "What鈥檚 new in Tornado 4.1") | - [previous](# "What鈥檚 new in Tornado 4.2.1") | - [Tornado 4.4.dev1 documentation](#) » - [Release notes](#) » # What's new in Tornado 4.2 ### May 26, 2015 ### Backwards-compatibility notes - `SSLIOStream.connect` and [`IOStream.start_tls`](# "tornado.iostream.IOStream.start_tls") now validate certificatesby default. - Certificate validation will now use the system CA root certificates insteadof `certifi` when possible (i.e. Python 2.7.9+ or 3.4+). This includes[`IOStream`](# "tornado.iostream.IOStream") and `simple_httpclient`, but not `curl_httpclient`. - The default SSL configuration has become stricter, using[`ssl.create_default_context`](https://docs.python.org/3.4/library/ssl.html#ssl.create_default_context "(in Python v3.4)") [https://docs.python.org/3.4/library/ssl.html#ssl.create_default_context] where available on the client side.(On the server side, applications are encouraged to migrate from the`ssl_options` dict-based API to pass an [`ssl.SSLContext`](https://docs.python.org/3.4/library/ssl.html#ssl.SSLContext "(in Python v3.4)") [https://docs.python.org/3.4/library/ssl.html#ssl.SSLContext] instead). - The deprecated classes in the [`tornado.auth`](# "tornado.auth") module, `GoogleMixin`,`FacebookMixin`, and `FriendFeedMixin` have been removed. ### New modules: [`tornado.locks`](# "tornado.locks") and [`tornado.queues`](# "tornado.queues") These modules provide classes for coordinating coroutines, merged from[Toro](http://toro.readthedocs.org) [http://toro.readthedocs.org]. To port your code from Toro's queues to Tornado 4.2, import [`Queue`](# "tornado.queues.Queue"),[`PriorityQueue`](# "tornado.queues.PriorityQueue"), or [`LifoQueue`](# "tornado.queues.LifoQueue") from [`tornado.queues`](# "tornado.queues") instead of from`toro`. Use [`Queue`](# "tornado.queues.Queue") instead of Toro's `JoinableQueue`. In Tornado the methods[`join`](# "tornado.queues.Queue.join") and [`task_done`](# "tornado.queues.Queue.task_done") are available on all queues, not on aspecial `JoinableQueue`. Tornado queues raise exceptions specific to Tornado instead of reusingexceptions from the Python standard library.Therefore instead of catching the standard [`queue.Empty`](https://docs.python.org/3.4/library/queue.html#queue.Empty "(in Python v3.4)") [https://docs.python.org/3.4/library/queue.html#queue.Empty] exception from[`Queue.get_nowait`](# "tornado.queues.Queue.get_nowait"), catch the special [`tornado.queues.QueueEmpty`](# "tornado.queues.QueueEmpty") exception,and instead of catching the standard [`queue.Full`](https://docs.python.org/3.4/library/queue.html#queue.Full "(in Python v3.4)") [https://docs.python.org/3.4/library/queue.html#queue.Full] from [`Queue.get_nowait`](# "tornado.queues.Queue.get_nowait"),catch [`tornado.queues.QueueFull`](# "tornado.queues.QueueFull"). To port from Toro's locks to Tornado 4.2, import [`Condition`](# "tornado.locks.Condition"), [`Event`](# "tornado.locks.Event"),[`Semaphore`](# "tornado.locks.Semaphore"), [`BoundedSemaphore`](# "tornado.locks.BoundedSemaphore"), or [`Lock`](# "tornado.locks.Lock") from [`tornado.locks`](# "tornado.locks")instead of from `toro`. Toro's `Semaphore.wait` allowed a coroutine to wait for the semaphore tobe unlocked *without* acquiring it. This encouraged unorthodox patterns; inTornado, just use [`acquire`](# "tornado.locks.Semaphore.acquire"). Toro's `Event.wait` raised a `Timeout` exception after a timeout. InTornado, [`Event.wait`](# "tornado.locks.Event.wait") raises [`tornado.gen.TimeoutError`](# "tornado.gen.TimeoutError"). Toro's `Condition.wait` also raised `Timeout`, but in Tornado, the [`Future`](# "tornado.concurrent.Future")returned by [`Condition.wait`](# "tornado.locks.Condition.wait") resolves to False after a timeout: ~~~ @gen.coroutine def await_notification(): if not (yield condition.wait(timeout=timedelta(seconds=1))): print('timed out') else: print('condition is true') ~~~ In lock and queue methods, wherever Toro accepted `deadline` as a keywordargument, Tornado names the argument `timeout` instead. Toro's `AsyncResult` is not merged into Tornado, nor its exceptions`NotReady` and `AlreadySet`. Use a [`Future`](# "tornado.concurrent.Future") instead. If you wrote code likethis: ~~~ 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". ~~~ Then the Tornado equivalent is: ~~~ 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`](# "tornado.autoreload") - Improved compatibility with Windows. - Fixed a bug in Python 3 if a module was imported during a reload check. ### [`tornado.concurrent`](# "tornado.concurrent") - [`run_on_executor`](# "tornado.concurrent.run_on_executor") now accepts arguments to control which attributesit uses to find the [`IOLoop`](# "tornado.ioloop.IOLoop") and executor. ### [`tornado.curl_httpclient`](# "tornado.curl_httpclient") - Fixed a bug that would cause the client to stop processing requestsif an exception occurred in certain places while there is a queue. ### [`tornado.escape`](# "tornado.escape") - [`xhtml_escape`](# "tornado.escape.xhtml_escape") now supports numeric character references in hexformat (` `) ### [`tornado.gen`](# "tornado.gen") - [`WaitIterator`](# "tornado.gen.WaitIterator") no longer uses weak references, which fixes severalgarbage-collection-related bugs. - `tornado.gen.Multi` and [`tornado.gen.multi_future`](# "tornado.gen.multi_future") (which are used whenyielding a list or dict in a coroutine) now log any exceptions after thefirst if more than one [`Future`](# "tornado.concurrent.Future") fails (previously they would be loggedwhen the [`Future`](# "tornado.concurrent.Future") was garbage-collected, but this is more reliable).Both have a new keyword argument `quiet_exceptions` to suppresslogging of certain exception types; to use this argument you mustcall `Multi` or `multi_future` directly instead of simply yieldinga list. - [`multi_future`](# "tornado.gen.multi_future") now works when given multiple copies of the same [`Future`](# "tornado.concurrent.Future"). - On Python 3, catching an exception in a coroutine no longer leads toleaks via `Exception.__context__`. ### [`tornado.httpclient`](# "tornado.httpclient") - The `raise_error` argument now works correctly with the synchronous[`HTTPClient`](# "tornado.httpclient.HTTPClient"). - The synchronous [`HTTPClient`](# "tornado.httpclient.HTTPClient") no longer interferes with [`IOLoop.current()`](# "tornado.ioloop.IOLoop.current"). ### [`tornado.httpserver`](# "tornado.httpserver") - [`HTTPServer`](# "tornado.httpserver.HTTPServer") is now a subclass of [`tornado.util.Configurable`](# "tornado.util.Configurable"). ### [`tornado.httputil`](# "tornado.httputil") - [`HTTPHeaders`](# "tornado.httputil.HTTPHeaders") can now be copied with [`copy.copy`](https://docs.python.org/3.4/library/copy.html#copy.copy "(in Python v3.4)") [https://docs.python.org/3.4/library/copy.html#copy.copy] and [`copy.deepcopy`](https://docs.python.org/3.4/library/copy.html#copy.deepcopy "(in Python v3.4)") [https://docs.python.org/3.4/library/copy.html#copy.deepcopy]. ### [`tornado.ioloop`](# "tornado.ioloop") - The [`IOLoop`](# "tornado.ioloop.IOLoop") constructor now has a `make_current` keyword argumentto control whether the new [`IOLoop`](# "tornado.ioloop.IOLoop") becomes [`IOLoop.current()`](# "tornado.ioloop.IOLoop.current"). - Third-party implementations of [`IOLoop`](# "tornado.ioloop.IOLoop") should accept `**kwargs`in their [`initialize`](# "tornado.ioloop.IOLoop.initialize") methods and pass them to the superclassimplementation. - [`PeriodicCallback`](# "tornado.ioloop.PeriodicCallback") is now more efficient when the clock jumps forwardby a large amount. ### [`tornado.iostream`](# "tornado.iostream") - `SSLIOStream.connect` and [`IOStream.start_tls`](# "tornado.iostream.IOStream.start_tls") now validate certificatesby default. - New method [`SSLIOStream.wait_for_handshake`](# "tornado.iostream.SSLIOStream.wait_for_handshake") allows server-side applicationsto wait for the handshake to complete in order to verify client certificatesor use NPN/ALPN. - The [`Future`](# "tornado.concurrent.Future") returned by `SSLIOStream.connect` now resolves after thehandshake is complete instead of as soon as the TCP connection isestablished. - Reduced logging of SSL errors. - [`BaseIOStream.read_until_close`](# "tornado.iostream.BaseIOStream.read_until_close") now works correctly when a`streaming_callback` is given but `callback` is None (i.e. whenit returns a [`Future`](# "tornado.concurrent.Future")) ### [`tornado.locale`](# "tornado.locale") - New method [`GettextLocale.pgettext`](# "tornado.locale.GettextLocale.pgettext") allows additional context to besupplied for gettext translations. ### [`tornado.log`](# "tornado.log") - [`define_logging_options`](# "tornado.log.define_logging_options") now works correctly when given a non-default`options` object. ### [`tornado.process`](# "tornado.process") - New method [`Subprocess.wait_for_exit`](# "tornado.process.Subprocess.wait_for_exit") is a coroutine-friendlyversion of [`Subprocess.set_exit_callback`](# "tornado.process.Subprocess.set_exit_callback"). ### [`tornado.simple_httpclient`](# "tornado.simple_httpclient") - Improved performance on Python 3 by reusing a single [`ssl.SSLContext`](https://docs.python.org/3.4/library/ssl.html#ssl.SSLContext "(in Python v3.4)") [https://docs.python.org/3.4/library/ssl.html#ssl.SSLContext]. - New constructor argument `max_body_size` controls the maximum responsesize the client is willing to accept. It may be bigger than`max_buffer_size` if `streaming_callback` is used. ### [`tornado.tcpserver`](# "tornado.tcpserver") - [`TCPServer.handle_stream`](# "tornado.tcpserver.TCPServer.handle_stream") may be a coroutine (so that any exceptionsit raises will be logged). ### [`tornado.util`](# "tornado.util") - [`import_object`](# "tornado.util.import_object") now supports unicode strings on Python 2. - [`Configurable.initialize`](# "tornado.util.Configurable.initialize") now supports positional arguments. ### [`tornado.web`](# "tornado.web") - Key versioning support for cookie signing. `cookie_secret` applicationsetting can now contain a dict of valid keys with version as key. Thecurrent signing key then must be specified via `key_version` setting. - Parsing of the `If-None-Match` header now follows the RFC and supportsweak validators. - Passing `secure=False` or `httponly=False` to[`RequestHandler.set_cookie`](# "tornado.web.RequestHandler.set_cookie") now works as expected (previously only thepresence of the argument was considered and its value was ignored). - [`RequestHandler.get_arguments`](# "tornado.web.RequestHandler.get_arguments") now requires that its `strip` argumentbe of type bool. This helps prevent errors caused by the slightly dissimilarinterfaces between the singular and plural methods. - Errors raised in `_handle_request_exception` are now logged more reliably. - [`RequestHandler.redirect`](# "tornado.web.RequestHandler.redirect") now works correctly when called from a handlerwhose path begins with two slashes. - Passing messages containing `%` characters to [`tornado.web.HTTPError`](# "tornado.web.HTTPError")no longer causes broken error messages. ### [`tornado.websocket`](# "tornado.websocket") - The `on_close` method will no longer be called more than once. - When the other side closes a connection, we now echo the received closecode back instead of sending an empty close frame. © Copyright 2009-2016, The Tornado Authors. Created using [Sphinx](http://sphinx-doc.org/) 1.3.5.