tornado icon indicating copy to clipboard operation
tornado copied to clipboard

Support streaming API for AsyncHTTPClient

Open legnaleurc opened this issue 8 years ago • 2 comments

Mostly like this: https://aiohttp.readthedocs.io/en/stable/client_quickstart.html#streaming-response-content

This could probably solve some usage like #157.

It is hard to control the process in streaming_callback, and it does not support async function as well.

legnaleurc avatar Dec 07 '17 08:12 legnaleurc

A seemingly simple fix is to wrap calls here with code like:

class _HTTPConnection(httputil.HTTPMessageDelegate):
    # ...
    def data_received(self, chunk: bytes) -> Optional[Awaitable[None]]:
        if self._should_follow_redirect():
            # We're going to follow a redirect so just discard the body.
            return None
        if self.request.streaming_callback is not None:
            # Should return either 'None' or a Future[None] to throttle future reads.
            return self.request.streaming_callback(chunk)
        self.chunks.append(chunk)
        return None

then to make sure that places that call: delegate.data_received(chunk) do the dance of:

res = delegate.data_received(chunk)
if res:
    await res  # or yield...

Wouldn't it be a bug if this behavior is not be supported, even though the parity of:

@web.stream_request_body
class Handler(tornado.web.RequestHandler):
    # ...
    async def data_received(self, chunk):
        # ....

is?

Additional library support (i.e. using httpx or similar) would work in some cases, but seeing that tornado has a curl client as well as this simple client already, it'd be nice to avoid additional dependencies for simple use-cases.

eulersIDcrisis avatar Mar 24 '25 21:03 eulersIDcrisis

If it is any help, I created a PR here: https://github.com/tornadoweb/tornado/pull/3471 It is currently in draft, but (after checking the linter state and so forth), I can mark it ready for review.

eulersIDcrisis avatar Mar 25 '25 22:03 eulersIDcrisis

Fixed by #3471

bdarnell avatar Sep 17 '25 17:09 bdarnell