igalshilman opened a new pull request #210:
URL: https://github.com/apache/flink-statefun/pull/210
Following the recent effort of standardizing the SDK surface for all the
languages, we need to update the Python SDK to reflect these changes.
The following is a short snippet copied from the README.md:
### A Mini-Tutorial
#### Define and Declare a Function
```
from 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 type `demo/greeter` and binds it to
the instance.
#### Registering and accessing persisted state
You 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 Handler
```
handler = RequestReplyHandler(functions)
```
#### Using the Handler with your Favorite HTTP Serving Framework
For 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 File
The remaining step would be to declare this function type in a module.yaml
```
functions:
- function:
meta:
kind: http
type: demo/greeter
spec:
endpoint: http://<end point url>/statefun
```
----------------------------------------------------------------
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:
[email protected]