igalshilman commented on a change in pull request #54: [FLINK-16515][docs] Refactor statefun documentation for multi-language SDKs URL: https://github.com/apache/flink-statefun/pull/54#discussion_r390257618
########## File path: statefun-docs/docs/sdk/python.rst ########## @@ -0,0 +1,170 @@ +.. Licensed to the Apache Software Foundation (ASF) under one + or more contributor license agreements. See the NOTICE file + distributed with this work for additional information + regarding copyright ownership. The ASF licenses this file + to you under the Apache License, Version 2.0 (the + "License"); you may not use this file except in compliance + with the License. You may obtain a copy of the License at + http://www.apache.org/licenses/LICENSE-2.0 + Unless required by applicable law or agreed to in writing, + software distributed under the License is distributed on an + "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + KIND, either express or implied. See the License for the + specific language governing permissions and limitations + under the License. + +.. _python: + +###### +Python +###### + +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 :ref:`remote_module`. + +To get started, add the Python SDK as a dependency to your application. + +.. code-block:: bash + + apache-flink-statefun=={version} + +.. contents:: :local: + +Defining A Stateful Function +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +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. + +.. literalinclude:: ../../src/main/python/HelloWorld.py + :language: python + :lines: 19- + +This code declares a function with in the namespace ``flink`` 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 :ref:`bottom of this page <context_reference>`. + +Type Hints +========== + +If the function has a static set of known supported types, they may be specified as `type hints <https://docs.python.org/3/library/typing.html>`_. +This includes `union types <https://docs.python.org/3/library/typing.html#typing.Union>`_ for functions that support multiple input message types. + + +.. literalinclude:: ../../src/main/python/TypeHint.py + :language: python + :lines: 19- + +Function Types and Messaging +^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +The decorator ``bind`` registers each function with the runtime under a function type. Review comment: perhaps we should also mention that it is also possible to call `functions.register(typename, fun)` is decorating is not possible. ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org With regards, Apache Git Services