From c45ce3accb9057c3401634570141c372ac591721 Mon Sep 17 00:00:00 2001 From: Ben Darnell Date: Sun, 25 Mar 2018 11:17:58 -0400 Subject: [PATCH] httpclient: Document inability to use sync HTTPClient in async app Eliminate a confusing error message when this occurs. Fixes #2325 --- tornado/httpclient.py | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/tornado/httpclient.py b/tornado/httpclient.py index 3547631da..9c438d15c 100644 --- a/tornado/httpclient.py +++ b/tornado/httpclient.py @@ -54,8 +54,10 @@ from tornado.util import Configurable class HTTPClient(object): """A blocking HTTP client. - This interface is provided for convenience and testing; most applications - that are running an IOLoop will want to use `AsyncHTTPClient` instead. + This interface is provided to make it easier to share code between + synchronous and asynchronous applications. Applications that are + running an `.IOLoop` must use `AsyncHTTPClient` instead. + Typical usage looks like this:: http_client = httpclient.HTTPClient() @@ -70,8 +72,19 @@ class HTTPClient(object): # Other errors are possible, such as IOError. print("Error: " + str(e)) http_client.close() + + .. versionchanged:: 5.0 + + Due to limitations in `asyncio`, it is no longer possible to + use the synchronous ``HTTPClient`` while an `.IOLoop` is running. + Use `AsyncHTTPClient` instead. + """ def __init__(self, async_client_class=None, **kwargs): + # Initialize self._closed at the beginning of the constructor + # so that an exception raised here doesn't lead to confusing + # failures in __del__. + self._closed = True self._io_loop = IOLoop(make_current=False) if async_client_class is None: async_client_class = AsyncHTTPClient -- 2.47.2