Skip to content

Commit 059f0e8

Browse files
committed
Rename WebSocketConnect to websocket_connect.
Add docs for undocumented functions and modules.
1 parent 6e3c288 commit 059f0e8

File tree

13 files changed

+105
-8
lines changed

13 files changed

+105
-8
lines changed

docs/concurrent.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
``tornado.concurrent`` --- Work with threads and futures
2+
========================================================
3+
4+
.. automodule:: tornado.concurrent
5+
:members:

docs/ioloop.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
.. automethod:: IOLoop.instance
1515
.. automethod:: IOLoop.initialized
1616
.. automethod:: IOLoop.install
17+
.. automethod:: IOLoop.current
18+
.. automethod:: IOLoop.make_current
1719
.. automethod:: IOLoop.start
1820
.. automethod:: IOLoop.stop
1921
.. automethod:: IOLoop.run_sync

docs/networking.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ Asynchronous networking
77
iostream
88
httpclient
99
netutil
10+
tcpserver

docs/releases/next.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,6 @@ Multiple modules
430430
~~~~~~~~~~~~~~~~~~~
431431

432432
* Client-side WebSocket support is now available:
433-
`tornado.websocket.WebSocketConnect`
433+
`tornado.websocket.websocket_connect`
434434
* `WebSocketHandler` has new methods `ping` and `on_pong` to send pings
435435
to the browser (not supported on the ``draft76`` protocol)

docs/tcpserver.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
``tornado.tcpserver`` --- Basic `IOStream`-based TCP server
2+
===========================================================
3+
4+
.. automodule:: tornado.tcpserver
5+
:members:

docs/utilities.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ Utilities
44
.. toctree::
55

66
autoreload
7+
concurrent
78
gen
89
httputil
910
log

docs/websocket.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,11 @@
3131
.. automethod:: WebSocketHandler.async_callback
3232
.. automethod:: WebSocketHandler.ping
3333
.. automethod:: WebSocketHandler.on_pong
34+
35+
36+
Client-side support
37+
-------------------
38+
39+
.. autofunction:: websocket_connect
40+
.. autoclass:: WebSocketClientConnection
41+
:members:

tornado/concurrent.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,11 @@ def submit(self, fn, *args, **kwargs):
132132

133133

134134
def run_on_executor(fn):
135+
"""Decorator to run a synchronous method asynchronously on an executor.
136+
137+
The decorated method may be called with a ``callback`` keyword
138+
argument and returns a future.
139+
"""
135140
@functools.wraps(fn)
136141
def wrapper(self, *args, **kwargs):
137142
callback = kwargs.pop("callback", None)
@@ -165,6 +170,7 @@ def return_future(f):
165170
`gen.engine` function, or passing it to `IOLoop.add_future`).
166171
167172
Usage::
173+
168174
@return_future
169175
def future_func(arg1, arg2, callback):
170176
# Do stuff (possibly asynchronous)

tornado/httpserver.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,11 @@ def __init__(self, stream, address, request_callback, no_keep_alive=False,
191191
self._close_callback = None
192192

193193
def set_close_callback(self, callback):
194+
"""Sets a callback that will be run when the connection is closed.
195+
196+
Use this instead of accessing `HTTPConnection.stream.set_close_callback`
197+
directly (which was the recommended approach prior to Tornado 3.0).
198+
"""
194199
self._close_callback = stack_context.wrap(callback)
195200
self.stream.set_close_callback(self._on_connection_close)
196201

tornado/ioloop.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,12 +163,31 @@ def install(self):
163163

164164
@staticmethod
165165
def current():
166+
"""Returns the current thread's `IOLoop`.
167+
168+
If an `IOLoop` is currently running or has been marked as current
169+
by `make_current`, returns that instance. Otherwise returns
170+
`IOLoop.instance()`, i.e. the main thread's `IOLoop`.
171+
172+
In general you should use `IOLoop.current` as the default when
173+
constructing an asynchronous object, and use `IOLoop.instance`
174+
when you mean to communicate to the main thread from a different
175+
one.
176+
"""
166177
current = getattr(IOLoop._current, "instance", None)
167178
if current is None:
168179
return IOLoop.instance()
169180
return current
170181

171182
def make_current(self):
183+
"""Makes this the `IOLoop` for the current thread.
184+
185+
An `IOLoop` automatically becomes current for its thread
186+
when it is started, but it is sometimes useful to call
187+
`make_current` explictly before starting the `IOLoop`,
188+
so that code run at startup time can find the right
189+
instance.
190+
"""
172191
IOLoop._current.instance = self
173192

174193
@staticmethod

tornado/netutil.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,22 @@ def is_valid_ip(ip):
160160

161161

162162
class Resolver(Configurable):
163+
"""Configurable asynchronous DNS resolver interface.
164+
165+
By default, a blocking implementation is used (which simply
166+
calls `socket.getaddrinfo`). An alternative implementation
167+
can be chosen with the `Resolver.configure` class method::
168+
169+
Resolver.configure('tornado.netutil.ThreadedResolver')
170+
171+
The implementations of this interface included with Tornado are
172+
173+
* `tornado.netutil.BlockingResolver`
174+
* `tornado.netutil.ThreadedResolver`
175+
* `tornado.netutil.OverrideResolver`
176+
* `tornado.platform.twisted.TwistedResolver`
177+
* `tornado.platform.caresresolver.CaresResolver`
178+
"""
163179
@classmethod
164180
def configurable_base(cls):
165181
return Resolver
@@ -199,11 +215,27 @@ def resolve(self, host, port, family=socket.AF_UNSPEC):
199215

200216

201217
class BlockingResolver(ExecutorResolver):
218+
"""Default `Resolver` implementation, using `socket.getaddrinfo`.
219+
220+
The `IOLoop` will be blocked during the resolution, although the
221+
callback will not be run until the next `IOLoop` iteration.
222+
"""
202223
def initialize(self, io_loop=None):
203224
super(BlockingResolver, self).initialize(io_loop=io_loop)
204225

205226

206227
class ThreadedResolver(ExecutorResolver):
228+
"""Multithreaded non-blocking `Resolver` implementation.
229+
230+
Requires the `concurrent.futures` package to be installed
231+
(available in the standard library since Python 3.2,
232+
installable with ``pip install futures`` in older versions).
233+
234+
The thread pool size can be configured with::
235+
236+
Resolver.configure('tornado.netutil.ThreadedResolver',
237+
num_threads=10)
238+
"""
207239
def initialize(self, io_loop=None, num_threads=10):
208240
from concurrent.futures import ThreadPoolExecutor
209241
super(ThreadedResolver, self).initialize(

tornado/test/websocket_test.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from tornado.testing import AsyncHTTPTestCase, gen_test
22
from tornado.web import Application
3-
from tornado.websocket import WebSocketHandler, WebSocketConnect
3+
from tornado.websocket import WebSocketHandler, websocket_connect
44

55

66
class EchoHandler(WebSocketHandler):
@@ -16,15 +16,15 @@ def get_app(self):
1616

1717
@gen_test
1818
def test_websocket_gen(self):
19-
ws = yield WebSocketConnect(
19+
ws = yield websocket_connect(
2020
'ws://localhost:%d/echo' % self.get_http_port(),
2121
io_loop=self.io_loop)
2222
ws.write_message('hello')
2323
response = yield ws.read_message()
2424
self.assertEqual(response, 'hello')
2525

2626
def test_websocket_callbacks(self):
27-
WebSocketConnect(
27+
websocket_connect(
2828
'ws://localhost:%d/echo' % self.get_http_port(),
2929
io_loop=self.io_loop, callback=self.stop)
3030
ws = self.wait().result()

tornado/websocket.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -718,7 +718,8 @@ def close(self):
718718
self.stream.io_loop.time() + 5, self._abort)
719719

720720

721-
class _WebSocketClientConnection(simple_httpclient._HTTPConnection):
721+
class WebSocketClientConnection(simple_httpclient._HTTPConnection):
722+
"""WebSocket client connection."""
722723
def __init__(self, io_loop, request):
723724
self.connect_future = Future()
724725
self.read_future = None
@@ -735,7 +736,7 @@ def __init__(self, io_loop, request):
735736
'Sec-WebSocket-Version': '13',
736737
})
737738

738-
super(_WebSocketClientConnection, self).__init__(
739+
super(WebSocketClientConnection, self).__init__(
739740
io_loop, None, request, lambda: None, lambda response: None,
740741
104857600, Resolver(io_loop=io_loop))
741742

@@ -759,9 +760,17 @@ def _handle_1xx(self, code):
759760
self.connect_future.set_result(self)
760761

761762
def write_message(self, message, binary=False):
763+
"""Sends a message to the WebSocket server."""
762764
self.protocol.write_message(message, binary)
763765

764766
def read_message(self, callback=None):
767+
"""Reads a message from the WebSocket server.
768+
769+
Returns a future whose result is the message, or None
770+
if the connection is closed. If a callback argument
771+
is given it will be called with the future when it is
772+
ready.
773+
"""
765774
assert self.read_future is None
766775
future = Future()
767776
if self.read_queue:
@@ -783,13 +792,17 @@ def on_pong(self, data):
783792
pass
784793

785794

786-
def WebSocketConnect(url, io_loop=None, callback=None):
795+
def websocket_connect(url, io_loop=None, callback=None):
796+
"""Client-side websocket support.
797+
798+
Takes a url and returns a Future whose result is a `WebSocketConnection`.
799+
"""
787800
if io_loop is None:
788801
io_loop = IOLoop.current()
789802
request = httpclient.HTTPRequest(url)
790803
request = httpclient._RequestProxy(
791804
request, httpclient.HTTPRequest._DEFAULTS)
792-
conn = _WebSocketClientConnection(io_loop, request)
805+
conn = WebSocketClientConnection(io_loop, request)
793806
if callback is not None:
794807
io_loop.add_future(conn.connect_future, callback)
795808
return conn.connect_future

0 commit comments

Comments
 (0)