# How to execute stateful Python functions?

**URL:** <https://community.palantir.com/t/how-to-execute-stateful-python-functions/6574>\
**Category:** Ask the Community\
**Tags:** python-functions\
**Created:** [May 12, 2026, 6:34pm UTC](https://community.palantir.com/t/how-to-execute-stateful-python-functions/6574 "2026-05-12T18:34:41Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![VincentF](https://avatars.discourse-cdn.com/v4/letter/v/8491ac/32.png) [@VincentF](https://community.palantir.com/u/VincentF)\
**Post date:** [May 12, 2026, 6:34pm UTC](https://community.palantir.com/t/how-to-execute-stateful-python-functions/6574/1 "2026-05-12T18:34:42Z")

</div>

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 ?

---

<div class="post-metadata">

**Author:** ![VincentF](https://avatars.discourse-cdn.com/v4/letter/v/8491ac/32.png) [@VincentF](https://community.palantir.com/u/VincentF)\
**Post date:** [May 12, 2026, 6:41pm UTC](https://community.palantir.com/t/how-to-execute-stateful-python-functions/6574/2 "2026-05-12T18:41:17Z")

</div>

This is possible with deployed functions.

For example:

```python

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}"
    )

```

 ![image](https://canada1.discourse-cdn.com/palantir/original/2X/9/9330d1777955f4c49e1bd6acec0ac1c8b3300c1a.png)  
 ![image](https://canada1.discourse-cdn.com/palantir/original/2X/b/be085aad1e1f0cab37d952cd457439a93cb84ae4.png)  
See https://www.palantir.com/docs/foundry/functions/functions-deployed

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

 ![image](https://canada1.discourse-cdn.com/palantir/original/2X/1/1f23d8e7a117f203c207bc9511554c28ff7679f0.png)

Whereas in serverless mode, the response stays:

 ![image](https://canada1.discourse-cdn.com/palantir/original/2X/1/13de03254550ed0d2986e6472282eedc60dfe10b.png)

---

<div class="post-metadata">

**Author:** ![system](https://yyz1.discourse-cdn.com/palantir/user_avatar/community.palantir.com/system/32/3_2.png) [@system](https://community.palantir.com/u/system)\
**Post date:** [August 12, 2026, 2:41am UTC](https://community.palantir.com/t/how-to-execute-stateful-python-functions/6574/3 "2026-08-12T02:41:47Z")

</div>

Closed on August 12, 2026, 2:41am UTC

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.
