Package: apache-flink-statefun
Apache Flink Stateful FunctionsStateful Functions is an API that simplifies the building ofdistributed stateful applicationswith aruntime built for serverless architectures.
It brings together the benefits of stateful stream processing - the processing of large datasets with low latency and bounded resource constraints -
along with a runtime for modeling stateful entities that supports location transparency, concurrency, scaling, and resiliency.It is designed to work with modern architectures, like cloud-native deployments and popular event-driven FaaS platforms
like AWS Lambda and KNative, and to provide out-of-the-box consistent state and messaging while preserving the serverless
experience and elasticity of these platforms.Stateful Functions is developed under the umbrella ofApache Flink.This README is meant as a brief walkthrough on the StateFun Python SDK and how to set things up
to get yourself started with Stateful Functions in Python.For a fully detailed documentation, please visit theofficial docs.For code examples, please take a look at theexamples.Table of ContentsPython SDK OverviewContributingLicensePython SDK OverviewBackgroundThe JVM-based Stateful Functions implementation has aRequestReplyextension (a protocol and an implementation) that allows calling into any HTTP endpoint that implements that protocol. Although it is possible to implement this protocol independently, this is a minimal library for the Python programing language that:Allows users to define and declare their functions in a convenient way.Dispatches an invocation request sent from the JVM to the appropriate function previously declared.A Mini-TutorialDefine and Declare a Functionfrom statefun import *

functions = StatefulFunctions()

@functions.bind(typename="demo/greeter")
def greet(context, message):
    print(f"Hey {message.as_string()}!")This code declares a function with of typedemo/greeterand binds it to the instance.Registering and accessing persisted stateYou can register persistent state that will be managed by the Stateful Functions workers
for state consistency and fault-tolerance. Values can be generally obtained via the context parameter:from statefun import *

functions = StatefulFunctions()

@functions.bind(
    typename="demo/greeter",
    specs=[ValueSpec(name="seen", type=IntType)])
def greet(context, message):
    seen = context.storage.seen or 0
    seen += 1
    context.storage.seen = seen
    print(f"Hey {message.as_string()} I've seen you {seen} times")Expose with a Request Reply Handlerhandler = RequestReplyHandler(functions)Using the Handler with your Favorite HTTP Serving FrameworkFor example, using Flask:@app.route('/statefun', methods=['POST'])
def handle():
    response_data = handler.handle_sync(request.data)
    response = make_response(response_data)
    response.headers.set('Content-Type', 'application/octet-stream')
    return response

if __name__ == "__main__":
    app.run()This creates an HTTP server that accepts requests from the Stateful Functions cluster and
dispatches it to the handler.Composing the Module YAML FileThe remaining step would be to declare this function type in a module.yamlfunctions:
  - function:
    meta:
      kind: http
      type: demo/greeter
    spec:
      endpoint: http://<end point url>/statefunTestingCreate a virtual environmentpython3 -m venv venv
source venv/bin/activateInstall dependenciespip3 install .Run unit testspython3 -m unittest testsContributingThere are multiple ways to enhance the Stateful Functions API for different types of applications; the runtime and operations will also evolve with the developments in Apache Flink.You can learn more about how to contribute in theApache Flink website. For code contributions, please read carefully theContributing Codesection and check theStateful Functionscomponent inJirafor an overview of ongoing community work.LicenseThe code in this repository is licensed under theApache Software License 2.