def encode_stream(self, encoding):
        """Encode a streaming response body.

        Use a generator wrapper, and just pray it works as the stream is
        being written out.
        """
        if encoding in self.attempted_charsets:
            return False
        self.attempted_charsets.add(encoding)

        def encoder(body):
            for chunk in body:
                if isinstance(chunk, str):
                    chunk = chunk.encode(encoding, self.errors)
                yield chunk

        self.body = encoder(self.body)
        return True
Encode a streaming response body.

        Use a generator wrapper, and just pray it works as the stream is
        being written out.
        
cherrypy/lib/encoding.py