worker.call(...).
The function on the other side receives an InputStream to read or an OutputStream to write. The
two functions can run in different processes and languages.
The examples use the stream API of the 0.27 SDKs. To write the functions that receive and return
streams, see Creating workers / Streaming. To move code from the
channel API, see Upgrading from 0.26.x. For how a stream works, see
Understanding iii / Streaming.
Send a stream in a call
To send a stream, put it in the payload of the call. For the steps that the SDK and the engine do, see How a stream works. Each SDK sends only specific types of values as streams. The supported types are listed in the comments on each language tab. Strings, buffers, plain objects, and arrays will not be sent as streams by default. To send these types as streams first convert them withInputStream.from().
- Node / TypeScript
- Python
- Rust
- Go
OutputStream: the output of worker.pipe(), or an OutputStream in the
result of a call. These are the operations of an OutputStream:
Read a stream from a result
A function can return a stream in its result. The result ofworker.call(...) then contains an
InputStream in the field where the function put the stream.
- Node / TypeScript
- Python
- Rust
- Go
InputStream from a result and the input of a pipe have the same read
operations. You can read an InputStream only once, so use the one operation that fits the data:
A stream can carry bytes and JSON messages. The operations that read bytes skip the messages, and
messages() skips the bytes. To read both types in the order in which the writer sent them, use
frames(). A frame is { type: "binary", value } for bytes, or { type: "text", value } for one
message as a string.
messages() parses each message as JSON. If a message is not valid JSON, messages() gives the
message as a string. For the two types of data, see
Bytes and messages.
Read a stream more than once
A second read of the sameInputStream fails with STREAM_IN_USE. To use the data in two places,
call tee() before the first read. tee() returns two copies of the stream, and each copy gives
all the data. The two copies are in the same process.
- Node / TypeScript
- Python
- Rust
- Go
tee() keeps at most 16 frames for the copy that is behind.
When one copy is 16 frames behind the other, the read of the faster copy waits. If you read one copy
to the end before you start the other, the first read waits after 16 frames and does not finish.
If you do not need one of the copies, call cancel() on it. The read of the other copy then does
not wait.
To send the data in another call, put one of the copies in the payload. The SDK refuses the stream
that you split with tee(), and the call fails with STREAM_IN_USE.
Receive data into a sink
A sink is a destination that receives the data of a stream, for example a file that is open for writing. Put the sink in the payload. The function receives it as anOutputStream and writes to
it, and the SDK copies the bytes into your destination. The call resolves after the sink has
received all the data, so the data is complete when the call returns.
- Node / TypeScript
- Python
- Rust
- Go
output of a pipe. See Create a pipe.
Sink errors
A call with a sink can fail in three ways:- The stream aborts. The call fails immediately with
STREAM_ABORTED. The call fails also when the function returned a result. - The sink fails. For example, the disk is full. The SDK stops the copy, and the call fails
immediately with
STREAM_ABORTED. Thereasonof the error starts withthe sink failed:, and thecauseof the error is the error from the sink. - The call fails. The SDK stops the copy and closes the sink with the error.
Sink timeout
ThetimeoutMs of a call includes the time that its sinks take to receive the data. The time starts
when the SDK sends the call. If the time expires before all the sinks are complete, the SDK aborts
the sinks that are still open, and the call fails with TIMEOUT.
Set a longer timeoutMs when a sink fills slowly. For example, some functions continue to write
after they return.
A call does not wait for its sinks when its result contains an OutputStream for you to write to.
The data that the sinks receive can depend on the data that you write, so the call resolves when the
function returns. A call with CallAction.Void does not wait for its sinks either.
In these two cases, a sink that aborts or fails after the call resolved cannot fail the call. A sink
that has already failed when the function returns still fails a call whose result contains an
OutputStream.
Create a pipe
When you put a stream or a sink in a payload, the SDK creates the pipe for you.worker.pipe() is
how you can create a pipe manually. It returns the two ends of the pipe: you write to output, and
you read the same data from input. Put one end in a payload, and use the other end in your code.
Use worker.pipe() in these cases:
- The function must receive a stream before you have its data.
- You make the call that uses the stream at a later time.
- You need the messages that the function sends. A sink keeps only the bytes and drops the messages.
- You need a two-way exchange. Use two pipes, one for each direction.
- Node / TypeScript
- Python
- Rust
- Go
worker.pipe({ bufferSize }). The maximum is 1024 frames.
Your worker owns each pipe that it creates with pipe(). See
Pipe ownership.
A function can fail before it writes to the output that you gave it. The output then stays open,
and your read of input waits until the engine drops the output connection. The error of the call
arrives first, so monitor the call while you read. When the call fails, you can stop the read with
input.cancel(). The read then fails with STREAM_CLOSED.
A pipe end is different from a sink. A call with a sink resolves after the stream ends. A call
with a pipe end resolves when the function returns.
Handle aborts and errors
A read of a stream ends in one of two ways. If the read ends without an error, the stream closed cleanly and you have all the data. If the stream aborts, the read gives you the data that the writer sent before the abort, and then throws aStreamAbortedError with the code STREAM_ABORTED. When
you get this error, the data is incomplete. Do not use it as the full stream.
- Node / TypeScript
- Python
- Rust
- Go
reason of the error does not contain the root cause of the failure. The reason comes from
the engine, for example channel writer aborted: closed with code 1011. To get the cause, read the
error or the result of the call. For how the engine relays an abort, see
Close and abort.
Error codes
The SDK reports each stream error as anInvocationError with one of these codes:
STREAM_ABORTED: The stream aborted before its end.STREAM_CLOSED: Awriteor asendcame after the stream closed, aborted, or lost its reader. A read also fails with this code when theInputStreamwas cancelled. A stream is cancelled bycancel(), by an early exit from a read loop, or when the handler that received it returns.STREAM_IN_USE: AnInputStreamwas read a second time, the same stream is in one payload two times, or the stream was already read to the end or already sent.STREAM_NOT_ALLOWED: A queued call has a stream in its payload. See Queue a call.
Queue a call
A queued call cannot carry a stream. If the payload of a call withCallAction.Enqueue contains a
stream, the SDK does not send the call, and the call fails with STREAM_NOT_ALLOWED. There are two
reasons:
- The queue can run the call after the pipe of the stream expired.
- When the queue runs a failed call again, the function cannot read the stream a second time.
CallAction.Void is different from CallAction.Enqueue. A call with CallAction.Void can carry
a stream. The function runs immediately, and the SDK does not wait for the result or for the sinks
of the call.Observe a stream
You can observe a function that receives or returns streams. The target of the observer does not receive the streams. In theinput or output that the observer captures, the target receives a
StreamInfo in place of each stream: { stream: true, direction, id }. A StreamInfo shows that
the call carried a stream, with the direction of the stream and the id of its pipe. It has no access
key, so the target cannot read or write the stream.
The other fields arrive as usual, and filters operate as usual. For the reason that the target
receives a StreamInfo, see
Streams and observers.
- Node / TypeScript
- Python
- Rust
- Go