Search code examples
async-awaittornadocoroutine

Tornado cancel httpclient.AsyncHTTPClient fetch() from on_chunk()


Inside one of the handlers I am doing the following:

    async def get(self):
          client = httpclient.AsyncHTTPClient()
          url = 'some url here'
          request = httpclient.HTTPRequest(url=url, streaming_callback=self.on_chunk, request_timeout=120)
          result = await client.fetch(request)
          self.write("done")


    @gen.coroutine
    def on_chunk(self, chunk):
          self.write(chunk)
          yield self.flush()

The requests can sometimes be quite large and the client may leave while the request is still in progress of being fetched and pumped to the client. If this happens an exception will appear in the on_chunk function when self.write() is attempted. My question is how do I abort the remaining download if my client went away ?


Solution

  • If your streaming_callback raises an exception, the client request should be aborted. This will spam the logs with stack traces, but there's not currently a cleaner way to do it. You can override on_connection_close to detect when the client has disconnected and set an attribute on self that you can check in on_chunk.