---
title: "Dynamic Invocation"
url: https://docs.zeroc.com/ice/3.8/runtime/invocation/dynamic-invocation
language: python
docs_index: https://docs.zeroc.com/llms.txt
---

> For an index of every page, see [llms.txt](https://docs.zeroc.com/llms.txt).

# Dynamic Invocation

Programming language: Python. Other languages: [C++](https://docs.zeroc.com/ice/3.8/runtime/invocation/dynamic-invocation/cpp.md), [C#](https://docs.zeroc.com/ice/3.8/runtime/invocation/dynamic-invocation/csharp.md), [Java](https://docs.zeroc.com/ice/3.8/runtime/invocation/dynamic-invocation/java.md), [Swift](https://docs.zeroc.com/ice/3.8/runtime/invocation/dynamic-invocation/swift.md).

A typed proxy marshals the parameters of an operation and unmarshals its results with code generated from the Slice definition of the operation. The [`ice_invoke`](https://code.zeroc.com/ice/3.8/api/python/Ice.ObjectPrx.html#Ice.ObjectPrx.ice_invoke) method of [ObjectPrx](https://code.zeroc.com/ice/3.8/api/python/Ice.ObjectPrx.html) invokes an operation by name instead: the application supplies the marshaled in-parameters and receives the marshaled reply. An application that forwards requests it does not unmarshal, or that calls operations without the generated code for the target interface, uses `ice_invoke`.

## Invoking an Operation

`ice_invoke` accepts the following arguments:

- the name of the operation
- the operation mode, `Normal` or `Idempotent`
- an encapsulation holding the marshaled in-parameters
- an optional [request context](https://docs.zeroc.com/ice/3.8/runtime/invocation/request-contexts/python.md)

`Ice.ObjectPrx` provides a synchronous [`ice_invoke`](https://code.zeroc.com/ice/3.8/api/python/Ice.ObjectPrx.html#Ice.ObjectPrx.ice_invoke) and an asynchronous [`ice_invokeAsync`](https://code.zeroc.com/ice/3.8/api/python/Ice.ObjectPrx.html#Ice.ObjectPrx.ice_invokeAsync).

### Operation Mode

The operation mode is `Idempotent` for a Slice operation declared [idempotent](https://docs.zeroc.com/ice/3.8/slice/operations/python.md#idempotent-operations), and `Normal` otherwise. The Ice runtime uses this mode to decide whether it can [retry](https://docs.zeroc.com/ice/3.8/runtime/invocation/automatic-retries/python.md#automatic-retries-for-idempotent-operations) an invocation that fails after the request was sent, and sends the mode with the request. Pass `Idempotent` when you know the operation is idempotent, or when you forward a request that arrived with this mode.

## Marshaling the In-Parameters

You marshal the in-parameters into an [encapsulation](https://docs.zeroc.com/ice/3.8/encoding/basic-data-encoding.md#encoding-for-encapsulations): the required parameters in the order of their declaration, followed by the optional parameters in the order of their tags, each marshaled as described in [Ice Encoding](https://docs.zeroc.com/ice/3.8/encoding.md). For an operation without in-parameters, you can pass an empty byte sequence; the Ice runtime then sends an empty encapsulation.

The Python mapping has no public stream API, so a Python application passes an encapsulation that it received already marshaled, such as the in-parameters of a request that it forwards.

```py
proxy = Ice.ObjectPrx(communicator, "greeter:tcp -h localhost -p 4061")
ok, outParams = await proxy.ice_invokeAsync("greet", Ice.OperationMode.Normal, inParams)
```

## Reading the Reply

For a twoway invocation, `ice_invoke` returns a success flag and an encapsulation:

- When the flag is `true`, the operation completed successfully, and the encapsulation holds the required out-parameters, followed by the required return value, followed by the optional out-parameters and optional return value in the order of their tags.
- When the flag is `false`, the operation completed with a user exception, and the encapsulation holds the [marshaled user exception](https://docs.zeroc.com/ice/3.8/encoding/data-encoding-for-exceptions.md).

For any other failure, `ice_invoke` throws a [local or dispatch exception](https://docs.zeroc.com/ice/3.8/runtime/local-and-dispatch-exceptions/python.md).

For a oneway or datagram invocation, `ice_invoke` returns `true` and an empty byte sequence once the transport accepts the request. For a batch oneway or batch datagram invocation, it returns `true` and an empty byte sequence once it adds the request to the batch; the Ice runtime sends the request when it [flushes](https://docs.zeroc.com/ice/3.8/runtime/invocation/invocation-mode/batched-invocations/python.md) the batch.

## Forwarding Requests

An application can forward the requests that its object adapter receives with `ice_invoke`, without unmarshaling their parameters. The forwarding code passes the encapsulation of the in-parameters from the incoming request to `ice_invoke` on the target proxy, and builds the response from the success flag and the encapsulation that `ice_invoke` returns. The operation name, mode and [context](https://docs.zeroc.com/ice/3.8/runtime/invocation/request-contexts/python.md) of the outgoing request are the forwarder's to set: a forwarder usually passes on those of the incoming request, and an intermediary such as Glacier2 adds or removes context entries along the way.

A forwarding servant derives from `Ice.Blobject` and implements its `ice_invoke` method, which receives the encapsulation of the in-parameters and returns the success flag and the encapsulation of the reply, or an awaitable for them, such as the one that `ice_invokeAsync` returns.

## See Also

- [Dispatcher API](https://docs.zeroc.com/ice/3.8/runtime/dispatch/dispatcher-api/python.md)
- [Basic Data Encoding](https://docs.zeroc.com/ice/3.8/encoding/basic-data-encoding.md)
- [Reply Message Body](https://docs.zeroc.com/ice/3.8/protocol/protocol-messages/python.md#reply-message-body)
