Request cancellation#
await invocation.cancel() sends an idempotent cancellation request for an accepted Invocation. The server returns a cancellation state projection. Cancellation can race with normal completion: check await invocation.status() or await invocation.result() to learn the terminal outcome.
invocation = await transform.remote_async(payload)
if should_stop_waiting:
await invocation.cancel()
else:
result = await invocation.result()
Cancellation does not turn the Invocation into a successful result. When it reaches the cancelled terminal outcome, Invocation.result() raises InvocationCancelledError. A configured execution deadline has the separate InvocationTimedOutError outcome.
Close a Task map#
Both synchronous and asynchronous Task.map iterators are lazy. Stop consuming them when the caller no longer needs more results. In async code, close the iterator in finally so the SDK stops pulling inputs and requests cancellation for accepted unfinished child Invocations:
results = transform.map.aio(items, max_in_flight=8)
try:
async for result in results:
if enough(result):
break
finally:
await results.aclose()
The close request applies to that map’s accepted unfinished children. It does not undo completed results or any external side effects the Tasks already performed. See Parallel Task map for ordering and error behavior.
Handle the outcome#
Treat cancellation as a request followed by an observed state. If the client loses its connection after requesting cancellation, reconnect and query the Invocation rather than assuming which side won the race. See Logs, Results & Failures for the exception taxonomy and Resource Usage for terminal usage.