Stateful functions are the building blocks of applications; they are atomic units of isolation, distribution, and persistence.
As objects, they encapsulate the state of a single entity (e.g., a specific user, device, or session) and encode its behavior.
Stateful functions can interact with each other, and external systems, through message passing.
The Python SDK is supported as a remote module.
To get started, add the Python SDK as a dependency to your application.
A stateful function is any function that that takes two parameters, a context and message.
The function is bound to the runtime through the stateful functions decorator.
The following is an example of a simple hello world function.
This code declares a function with in the namespace example and of type hello and binds it to the hello_function Python instance.
Messages’s are untyped and passed through the system as google.protobuf.Any so one function can potentially process multiple types of messages.
The context provides metadata about the current message and function, and is how you can call other functions or external systems.
A full reference of all methods supported by the context object are listed at the bottom of this page.
Type Hints
If the function has a static set of known supported types, they may be specified as type hints.
This includes union types for functions that support multiple input message types.
Function Types and Messaging
The decorator bind registers each function with the runtime under a function type.
The function type must take the form <namespace>/<name>.
Function types can then be referenced from other functions to create an address and message a particular instance.
Alternatively, functions can be manually bound to the runtime.
Sending Delayed Messages
Functions are able to send messages on a delay so that they will arrive after some duration.
Functions may even send themselves delayed messages that can serve as a callback.
The delayed message is non-blocking so functions will continue to process records between the time a delayed message is sent and received.
The delay is specified via a Python timedelta.
Persistence
Stateful Functions treats state as a first class citizen and so all stateful functions can easily define state that is automatically made fault tolerant by the runtime.
All stateful functions may contain state by merely storing values within the context object.
The data is always scoped to a specific function type and identifier.
State values could be absent, None, or a google.protobuf.Any.
Attention: [Remote modules](//ci.apache.org/projects/flink/flink-statefun-docs-release-2.0/sdk/modules.html#remote-module) require that all state values are eagerly registered at module.yaml.
Below is a stateful function that greets users based on the number of times they have been seen.
Additionally, persisted values may be cleared by deleting its value.
Exposing Functions
The Python SDK ships with a RequestReplyHandler that automatically dispatches function calls based on RESTful HTTP POSTS.
The RequestReplyHandler may be exposed using any HTTP framework.
Serving Functions With Flask
One popular Python web framework is Flask.
It can be used to quickly and easily expose a RequestResponseHandler.
Context Reference
The context object passed to each function has the following attributes / methods.
send(self, typename: str, id: str, message: Any)
Send a message to any function with the function type of the the form <namesapce>/<type> and message of type google.protobuf.Any