def close(self):
        """Close the stream.

        If *autoclose* was passed to the constructor then the underlying
        transport will be closed as well.
        """
        if self._closed:
            return
        if self._autoclose:
            self._transport.close()
            self._transport._closed.wait()
        self._transport = None
        self._closed = True