How to execute stateful Python functions?

I want to execute stateful functions. For example, that could share a common cache or counter, between runs.

For instance, it could only query the ontology once, keep a temporary cache, and serve a few calls before refreshing the ontology values.

How can I do so ?

This is possible with deployed functions.

For example:


from functions.api import function
from datetime import datetime
# ---------------------------------------------------------------------------
# Module-level state
#
# In a *deployed* Python Function the module is loaded once when the container
# starts and stays alive for the lifetime of the deployment.  Any module-level
# variable therefore persists across successive invocations (runs) of the
# function – exactly like a global variable in a long-running server process.
#
# ⚠️  Important caveats
#   • This state is IN-MEMORY ONLY.  It is lost when the deployment is
#     restarted, upgraded, or scaled to a new instance.
#   • *Serverless* functions do NOT share state between calls because each
#     invocation may be handled by a fresh process.  Deploy the function
#     (non-serverless) to observe the stateful behaviour.
#   • There is no cross-replica sharing: if the deployment is scaled to
#     multiple replicas each replica maintains its own independent counter.
# ---------------------------------------------------------------------------
_invocation_count: int = 0
_invocation_history: list[str] = []


@function
def stateful_counter(label: str = "ping") -> str:
    """
    Increment a module-level counter every time this function is called and
    return a summary of all previous calls.

    Call this function several times (e.g. from Live Preview or Workshop) and
    watch the counter climb – proving that state is preserved between runs on
    a *deployed* function instance.

    Args:
        label: An optional tag that is recorded alongside the timestamp so you
               can distinguish individual calls.

    Returns:
        A human-readable string showing the current call number and the full
        call history accumulated since the deployment started.
    """
    global _invocation_count, _invocation_history

    _invocation_count += 1
    timestamp = datetime.utcnow().isoformat(timespec="seconds") + "Z"
    _invocation_history.append(f"#{_invocation_count} [{timestamp}] label='{label}'")

    history_lines = "\n  ".join(_invocation_history)
    return (
        f"This deployed instance has been called {_invocation_count} time(s).\n\n"
        f"Full call history:\n  {history_lines}"
    )



See https://www.palantir.com/docs/foundry/functions/functions-deployed

For example from workshop, once the function is deployed, that’s what I see:

Whereas in serverless mode, the response stays: