Home

readuntil

Readuntil is a coroutine method of asyncio.StreamReader in Python’s standard library. It reads data from an asynchronous stream until the specified separator bytes are encountered, returning the accumulated data including the separator.

If the end of the stream is reached before the separator is found, readuntil raises asyncio.IncompleteReadError,

Usage typically appears in async protocols where messages are delimited by a known sequence, such as a

Relation to other methods: readuntil differs from readexactly in that it waits for a delimiter rather than

Limitations and considerations: care is needed to ensure the chosen delimiter reliably appears in the stream

which
carries
any
partial
data
that
was
read.
If
the
search
exceeds
the
configured
limit
without
locating
the
separator,
it
raises
asyncio.LimitOverrunError.
The
method
respects
the
StreamReader’s
limit,
which
defaults
to
65536
bytes
but
can
be
adjusted
when
creating
the
reader
or
by
setting
the
limit
parameter.
newline
or
a
custom
delimiter.
For
example,
data
=
await
reader.readuntil(b'\n')
reads
a
single
line
terminated
by
a
newline.
The
returned
bytes
include
the
delimiter,
and
decoding
to
text
is
usually
performed
after
retrieval,
e.g.,
line
=
data.decode().
a
fixed
byte
count,
and
it
differs
from
readline
in
that
the
delimiter
can
be
any
specified
sequence
rather
than
only
a
newline.
Readuntil
is
part
of
the
broader
asyncio
Streams
API
available
in
Python
3.5
and
later,
and
is
commonly
used
in
network
clients
and
servers
that
exchange
delimited
messages.
and
to
handle
possible
exceptions
(IncompleteReadError,
LimitOverrunError)
in
error
handling
paths.