ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、视频、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
### Navigation - [index](# "General Index") - [modules](# "Python Module Index") | - [next](# "What鈥檚 new in Tornado 3.2.2") | - [previous](# "What鈥檚 new in Tornado 4.0.1") | - [Tornado 4.4.dev1 documentation](#) » - [Release notes](#) » # What's new in Tornado 4.0 ### July 15, 2014 ### Highlights - The [`tornado.web.stream_request_body`](# "tornado.web.stream_request_body") decorator allows large files to beuploaded with limited memory usage. - Coroutines are now faster and are used extensively throughout Tornado itself.More methods now return [`Futures`](# "tornado.concurrent.Future"), including most [`IOStream`](# "tornado.iostream.IOStream")methods and [`RequestHandler.flush`](# "tornado.web.RequestHandler.flush"). - Many user-overridden methods are now allowed to return a [`Future`](# "tornado.concurrent.Future")for flow control. - HTTP-related code is now shared between the [`tornado.httpserver`](# "tornado.httpserver"),`tornado.simple_httpclient` and [`tornado.wsgi`](# "tornado.wsgi") modules, making supportfor features such as chunked and gzip encoding more consistent.[`HTTPServer`](# "tornado.httpserver.HTTPServer") now uses new delegate interfaces defined in [`tornado.httputil`](# "tornado.httputil")in addition to its old single-callback interface. - New module [`tornado.tcpclient`](# "tornado.tcpclient") creates TCP connections with non-blockingDNS, SSL handshaking, and support for IPv6. ### Backwards-compatibility notes - [`tornado.concurrent.Future`](# "tornado.concurrent.Future") is no longer thread-safe; use[`concurrent.futures.Future`](https://docs.python.org/3.4/library/concurrent.futures.html#concurrent.futures.Future "(in Python v3.4)") [https://docs.python.org/3.4/library/concurrent.futures.html#concurrent.futures.Future] when thread-safety is needed. - Tornado now depends on the [certifi](https://pypi.python.org/pypi/certifi) [https://pypi.python.org/pypi/certifi]package instead of bundling its own copy of the Mozilla CA list. This willbe installed automatically when using `pip` or `easy_install`. - This version includes the changes to the secure cookie format firstintroduced in version [*3.2.1*](#), and the xsrf token changein version [*3.2.2*](#). If you are upgrading from an earlierversion, see those versions' release notes. - WebSocket connections from other origin sites are now rejected by default.To accept cross-origin websocket connections, overridethe new method [`WebSocketHandler.check_origin`](# "tornado.websocket.WebSocketHandler.check_origin"). - [`WebSocketHandler`](# "tornado.websocket.WebSocketHandler") no longer supports the old `draft 76` protocol(this mainly affects Safari 5.x browsers). Applications should usenon-websocket workarounds for these browsers. - Authors of alternative [`IOLoop`](# "tornado.ioloop.IOLoop") implementations should see the changesto [`IOLoop.add_handler`](# "tornado.ioloop.IOLoop.add_handler") in this release. - The `RequestHandler.async_callback` and `WebSocketHandler.async_callback`wrapper functions have been removed; they have been obsolete for a longtime due to stack contexts (and more recently coroutines). - `curl_httpclient` now requires a minimum of libcurl version 7.21.1 andpycurl 7.18.2. - Support for `RequestHandler.get_error_html` has been removed;override [`RequestHandler.write_error`](# "tornado.web.RequestHandler.write_error") instead. ### Other notes - The git repository has moved to [https://github.com/tornadoweb/tornado](https://github.com/tornadoweb/tornado).All old links should be redirected to the new location. - An [announcement mailing list](http://groups.google.com/group/python-tornado-announce) [http://groups.google.com/group/python-tornado-announce] is now available. - All Tornado modules are now importable on Google App Engine (althoughthe App Engine environment does not allow the system calls usedby [`IOLoop`](# "tornado.ioloop.IOLoop") so many modules are still unusable). ### [`tornado.auth`](# "tornado.auth") - Fixed a bug in `.FacebookMixin` on Python 3. - When using the [`Future`](# "tornado.concurrent.Future") interface, exceptions are more reliably deliveredto the caller. ### [`tornado.concurrent`](# "tornado.concurrent") - [`tornado.concurrent.Future`](# "tornado.concurrent.Future") is now always thread-unsafe (previouslyit would be thread-safe if the [`concurrent.futures`](https://docs.python.org/3.4/library/concurrent.futures.html#module-concurrent.futures "(in Python v3.4)") [https://docs.python.org/3.4/library/concurrent.futures.html#module-concurrent.futures] package was available).This improves performance and provides more consistent semantics.The parts of Tornado that accept Futures will accept both Tornado'sthread-unsafe Futures and the thread-safe [`concurrent.futures.Future`](https://docs.python.org/3.4/library/concurrent.futures.html#concurrent.futures.Future "(in Python v3.4)") [https://docs.python.org/3.4/library/concurrent.futures.html#concurrent.futures.Future]. - [`tornado.concurrent.Future`](# "tornado.concurrent.Future") now includes all the functionalityof the old `TracebackFuture` class. `TracebackFuture` is nowsimply an alias for `Future`. ### `tornado.curl_httpclient` - `curl_httpclient` now passes along the HTTP “reason” stringin `response.reason`. ### [`tornado.gen`](# "tornado.gen") - Performance of coroutines has been improved. - Coroutines no longer generate `StackContexts` by default, but theywill be created on demand when needed. - The internals of the [`tornado.gen`](# "tornado.gen") module have been rewritten toimprove performance when using `Futures`, at the expense of someperformance degradation for the older [`YieldPoint`](# "tornado.gen.YieldPoint") interfaces. - New function [`with_timeout`](# "tornado.gen.with_timeout") wraps a [`Future`](# "tornado.concurrent.Future") and raises an exceptionif it doesn't complete in a given amount of time. - New object [`moment`](# "tornado.gen.moment") can be yielded to allow the IOLoop to run forone iteration before resuming. - [`Task`](# "tornado.gen.Task") is now a function returning a [`Future`](# "tornado.concurrent.Future") instead of a [`YieldPoint`](# "tornado.gen.YieldPoint")subclass. This change should be transparent to application code, butallows [`Task`](# "tornado.gen.Task") to take advantage of the newly-optimized [`Future`](# "tornado.concurrent.Future")handling. ### [`tornado.http1connection`](# "tornado.http1connection") - New module contains the HTTP implementation shared by [`tornado.httpserver`](# "tornado.httpserver")and `tornado.simple_httpclient`. ### [`tornado.httpclient`](# "tornado.httpclient") - The command-line HTTP client (`python -m tornado.httpclient $URL`)now works on Python 3. - Fixed a memory leak in [`AsyncHTTPClient`](# "tornado.httpclient.AsyncHTTPClient") shutdown that affectedapplications that created many HTTP clients and IOLoops. - New client request parameter `decompress_response` replacesthe existing `use_gzip` parameter; both names are accepted. ### [`tornado.httpserver`](# "tornado.httpserver") - `tornado.httpserver.HTTPRequest` has moved to[`tornado.httputil.HTTPServerRequest`](# "tornado.httputil.HTTPServerRequest"). - HTTP implementation has been unified with `tornado.simple_httpclient`in [`tornado.http1connection`](# "tornado.http1connection"). - Now supports `Transfer-Encoding: chunked` for request bodies. - Now supports `Content-Encoding: gzip` for request bodies if`decompress_request=True` is passed to the [`HTTPServer`](# "tornado.httpserver.HTTPServer") constructor. - The `connection` attribute of [`HTTPServerRequest`](# "tornado.httputil.HTTPServerRequest") is now documentedfor public use; applications are expected to write their responsesvia the [`HTTPConnection`](# "tornado.httputil.HTTPConnection") interface. - The [`HTTPServerRequest.write`](# "tornado.httputil.HTTPServerRequest.write") and [`HTTPServerRequest.finish`](# "tornado.httputil.HTTPServerRequest.finish") methodsare now deprecated. ([`RequestHandler.write`](# "tornado.web.RequestHandler.write") and [`RequestHandler.finish`](# "tornado.web.RequestHandler.finish")are *not* deprecated; this only applies to the methods on[`HTTPServerRequest`](# "tornado.httputil.HTTPServerRequest")) - [`HTTPServer`](# "tornado.httpserver.HTTPServer") now supports [`HTTPServerConnectionDelegate`](# "tornado.httputil.HTTPServerConnectionDelegate") in addition tothe old `request_callback` interface. The delegate interface supportsstreaming of request bodies. - [`HTTPServer`](# "tornado.httpserver.HTTPServer") now detects the error of an application sending a`Content-Length` error that is inconsistent with the actual content. - New constructor arguments `max_header_size` and `max_body_size`allow separate limits to be set for different parts of the request.`max_body_size` is applied even in streaming mode. - New constructor argument `chunk_size` can be used to limit the amountof data read into memory at one time per request. - New constructor arguments `idle_connection_timeout` and `body_timeout`allow time limits to be placed on the reading of requests. - Form-encoded message bodies are now parsed for all HTTP methods, not just`POST`, `PUT`, and `PATCH`. ### [`tornado.httputil`](# "tornado.httputil") - [`HTTPServerRequest`](# "tornado.httputil.HTTPServerRequest") was moved to this module from [`tornado.httpserver`](# "tornado.httpserver"). - New base classes [`HTTPConnection`](# "tornado.httputil.HTTPConnection"), [`HTTPServerConnectionDelegate`](# "tornado.httputil.HTTPServerConnectionDelegate"),and [`HTTPMessageDelegate`](# "tornado.httputil.HTTPMessageDelegate") define the interaction between applicationsand the HTTP implementation. ### [`tornado.ioloop`](# "tornado.ioloop") - [`IOLoop.add_handler`](# "tornado.ioloop.IOLoop.add_handler") and related methods now accept file-like objectsin addition to raw file descriptors. Passing the objects is recommended(when possible) to avoid a garbage-collection-related problem in unit tests. - New method [`IOLoop.clear_instance`](# "tornado.ioloop.IOLoop.clear_instance") makes it possible to uninstall thesingleton instance. - Timeout scheduling is now more robust against slow callbacks. - [`IOLoop.add_timeout`](# "tornado.ioloop.IOLoop.add_timeout") is now a bit more efficient. - When a function run by the [`IOLoop`](# "tornado.ioloop.IOLoop") returns a [`Future`](# "tornado.concurrent.Future") and that [`Future`](# "tornado.concurrent.Future")has an exception, the [`IOLoop`](# "tornado.ioloop.IOLoop") will log the exception. - New method [`IOLoop.spawn_callback`](# "tornado.ioloop.IOLoop.spawn_callback") simplifies the process of launchinga fire-and-forget callback that is separated from the caller's stack context. - New methods [`IOLoop.call_later`](# "tornado.ioloop.IOLoop.call_later") and [`IOLoop.call_at`](# "tornado.ioloop.IOLoop.call_at") simplify thespecification of relative or absolute timeouts (as opposed to[`add_timeout`](# "tornado.ioloop.IOLoop.add_timeout"), which used the type of its argument). ### [`tornado.iostream`](# "tornado.iostream") - The `callback` argument to most [`IOStream`](# "tornado.iostream.IOStream") methods is now optional.When called without a callback the method will return a [`Future`](# "tornado.concurrent.Future")for use with coroutines. - New method [`IOStream.start_tls`](# "tornado.iostream.IOStream.start_tls") converts an [`IOStream`](# "tornado.iostream.IOStream") to an[`SSLIOStream`](# "tornado.iostream.SSLIOStream"). - No longer gets confused when an `IOError` or `OSError` withoutan `errno` attribute is raised. - [`BaseIOStream.read_bytes`](# "tornado.iostream.BaseIOStream.read_bytes") now accepts a `partial` keyword argument,which can be used to return before the full amount has been read.This is a more coroutine-friendly alternative to `streaming_callback`. - [`BaseIOStream.read_until`](# "tornado.iostream.BaseIOStream.read_until") and `read_until_regex` now acept a`max_bytes` keyword argument which will cause the request to fail ifit cannot be satisfied from the given number of bytes. - [`IOStream`](# "tornado.iostream.IOStream") no longer reads from the socket into memory if it does notneed data to satisfy a pending read. As a side effect, the close callbackwill not be run immediately if the other side closes the connectionwhile there is unconsumed data in the buffer. - The default `chunk_size` has been increased to 64KB (from 4KB) - The [`IOStream`](# "tornado.iostream.IOStream") constructor takes a new keyword argument`max_write_buffer_size` (defaults to unlimited). Calls to[`BaseIOStream.write`](# "tornado.iostream.BaseIOStream.write") will raise [`StreamBufferFullError`](# "tornado.iostream.StreamBufferFullError") if the amountof unsent buffered data exceeds this limit. - `ETIMEDOUT` errors are no longer logged. If you need to distinguishtimeouts from other forms of closed connections, examine `stream.error`from a close callback. ### [`tornado.netutil`](# "tornado.netutil") - When [`bind_sockets`](# "tornado.netutil.bind_sockets") chooses a port automatically, it will now usethe same port for IPv4 and IPv6. - TLS compression is now disabled by default on Python 3.3 and higher(it is not possible to change this option in older versions). ### [`tornado.options`](# "tornado.options") - It is now possible to disable the default logging configurationby setting `options.logging` to `None` instead of the string `"none"`. ### [`tornado.platform.asyncio`](# "tornado.platform.asyncio") - Now works on Python 2.6. - Now works with Trollius version 0.3. ### [`tornado.platform.twisted`](# "tornado.platform.twisted") - [`TwistedIOLoop`](# "tornado.platform.twisted.TwistedIOLoop") now works on Python 3.3+ (with Twisted 14.0.0+). ### `tornado.simple_httpclient` - `simple_httpclient` has better support for IPv6, which is now enabledby default. - Improved default cipher suite selection (Python 2.7+). - HTTP implementation has been unified with `tornado.httpserver`in [`tornado.http1connection`](# "tornado.http1connection") - Streaming request bodies are now supported via the `body_producer`keyword argument to [`tornado.httpclient.HTTPRequest`](# "tornado.httpclient.HTTPRequest"). - The `expect_100_continue` keyword argument to[`tornado.httpclient.HTTPRequest`](# "tornado.httpclient.HTTPRequest") allows the use of the HTTP `Expect:100-continue` feature. - `simple_httpclient` now raises the original exception (e.g. an [`IOError`](https://docs.python.org/3.4/library/exceptions.html#IOError "(in Python v3.4)") [https://docs.python.org/3.4/library/exceptions.html#IOError])in more cases, instead of converting everything to `HTTPError`. ### [`tornado.stack_context`](# "tornado.stack_context") - The stack context system now has less performance overhead when nostack contexts are active. ### [`tornado.tcpclient`](# "tornado.tcpclient") - New module which creates TCP connections and IOStreams, includingname resolution, connecting, and SSL handshakes. ### [`tornado.testing`](# "tornado.testing") - [`AsyncTestCase`](# "tornado.testing.AsyncTestCase") now attempts to detect test methods that are generatorsbut were not run with `@gen_test` or any similar decorator (this wouldpreviously result in the test silently being skipped). - Better stack traces are now displayed when a test times out. - The `@gen_test` decorator now passes along `*args, **kwargs` so itcan be used on functions with arguments. - Fixed the test suite when `unittest2` is installed on Python 3. ### [`tornado.web`](# "tornado.web") - It is now possible to support streaming request bodies with the[`stream_request_body`](# "tornado.web.stream_request_body") decorator and the new [`RequestHandler.data_received`](# "tornado.web.RequestHandler.data_received")method. - [`RequestHandler.flush`](# "tornado.web.RequestHandler.flush") now returns a [`Future`](# "tornado.concurrent.Future") if no callback is given. - New exception [`Finish`](# "tornado.web.Finish") may be raised to finish a request withouttriggering error handling. - When gzip support is enabled, all `text/*` mime types will be compressed,not just those on a whitelist. - [`Application`](# "tornado.web.Application") now implements the [`HTTPMessageDelegate`](# "tornado.httputil.HTTPMessageDelegate") interface. - `HEAD` requests in [`StaticFileHandler`](# "tornado.web.StaticFileHandler") no longer read the entire file. - [`StaticFileHandler`](# "tornado.web.StaticFileHandler") now streams response bodies to the client. - New setting `compress_response` replaces the existing `gzip`setting; both names are accepted. - XSRF cookies that were not generated by this module (i.e. strings withoutany particular formatting) are once again accepted (as long as thecookie and body/header match). This pattern was common fortesting and non-browser clients but was broken by the changes inTornado 3.2.2. ### [`tornado.websocket`](# "tornado.websocket") - WebSocket connections from other origin sites are now rejected by default.Browsers do not use the same-origin policy for WebSocket connections as theydo for most other browser-initiated communications. This can be surprisingand a security risk, so we disallow these connections on the server sideby default. To accept cross-origin websocket connections, overridethe new method [`WebSocketHandler.check_origin`](# "tornado.websocket.WebSocketHandler.check_origin"). - [`WebSocketHandler.close`](# "tornado.websocket.WebSocketHandler.close") and [`WebSocketClientConnection.close`](# "tornado.websocket.WebSocketClientConnection.close") nowsupport `code` and `reason` arguments to send a status code andmessage to the other side of the connection when closing. Both classesalso have `close_code` and `close_reason` attributes to receive thesevalues when the other side closes. - The C speedup module now builds correctly with MSVC, and can supportmessages larger than 2GB on 64-bit systems. - The fallback mechanism for detecting a missing C compiler nowworks correctly on Mac OS X. - Arguments to [`WebSocketHandler.open`](# "tornado.websocket.WebSocketHandler.open") are now decoded in the same wayas arguments to [`RequestHandler.get`](# "tornado.web.RequestHandler.get") and similar methods. - It is now allowed to override `prepare` in a [`WebSocketHandler`](# "tornado.websocket.WebSocketHandler"),and this method may generate HTTP responses (error pages) in the usualway. The HTTP response methods are still not allowed once theWebSocket handshake has completed. ### [`tornado.wsgi`](# "tornado.wsgi") - New class [`WSGIAdapter`](# "tornado.wsgi.WSGIAdapter") supports running a Tornado [`Application`](# "tornado.web.Application") ona WSGI server in a way that is more compatible with Tornado's non-WSGI[`HTTPServer`](# "tornado.httpserver.HTTPServer"). [`WSGIApplication`](# "tornado.wsgi.WSGIApplication") is deprecated in favor of using[`WSGIAdapter`](# "tornado.wsgi.WSGIAdapter") with a regular [`Application`](# "tornado.web.Application"). - [`WSGIAdapter`](# "tornado.wsgi.WSGIAdapter") now supports gzipped output. © Copyright 2009-2016, The Tornado Authors. Created using [Sphinx](http://sphinx-doc.org/) 1.3.5.