def read(self, nbytes, timeout=None):
out = bytes()<EOL>self._lock.acquire()<EOL>try:<EOL><INDENT>if len(self._buffer) == <NUM_LIT:0>:<EOL><INDENT>if self._closed:<EOL><INDENT>return out<EOL><DEDENT>if timeout == <NUM_LIT:0.0>:<EOL><INDENT>raise PipeTimeout()<EOL><DEDENT>while (len(self._buffer) == <NUM_LIT:0>) and not self._closed:<EOL><INDENT>then = time.time()<EOL>self._cv.wait(timeout)<EOL>if timeout is not None:<EOL><INDENT>timeout -= time.time() - then<EOL>if timeout <= <NUM_LIT:0.0>:<EOL><INDENT>raise PipeTimeout()<EOL><DEDENT><DEDENT><DEDENT><DEDENT>if len(self._buffer) <= nbytes:<EOL><INDENT>out = self._buffer_tobytes()<EOL>del self._buffer[:]<EOL>if (self._event is not None) and not self._closed:<EOL><INDENT>self._event.clear()<EOL><DEDENT><DEDENT>else:<EOL><INDENT>out = self._buffer_tobytes(nbytes)<EOL>del self._buffer[:nbytes]<EOL><DEDENT><DEDENT>finally:<EOL><INDENT>self._lock.release()<EOL><DEDENT>return out<EOL>
Read data from the pipe.  The return value is a string representing
the data received.  The maximum amount of data to be received at once
is specified by ``nbytes``.  If a string of length zero is returned,
the pipe has been closed.

The optional ``timeout`` argument can be a nonnegative float expressing
seconds, or ``None`` for no timeout.  If a float is given, a
`.PipeTimeout` will be raised if the timeout period value has elapsed
before any data arrives.

:param int nbytes: maximum number of bytes to read
:param float timeout:
    maximum seconds to wait (or ``None``, the default, to wait forever)
:return: the read data, as a ``str`` or ``bytes``

:raises:
    `.PipeTimeout` -- if a timeout was specified and no data was ready
    before that timeout