|
1 | 1 | # Spawn Python SDK |
2 | | - |
3 | 2 | Python User Language Support for [Spawn](https://github.com/eigr/spawn). |
4 | 3 |
|
5 | | -## Installation via source |
| 4 | +# Table of Contents |
| 5 | +1. [Overview](#overview) |
| 6 | +2. [Getting Started](#getting-started) |
| 7 | + |
| 8 | + |
| 9 | +## Overview |
| 10 | +TODO |
| 11 | + |
| 12 | +## Getting Started |
| 13 | + |
| 14 | +First we must create a new Python project. In this example we will use [Poetry](https://python-poetry.org) as our package manager. |
6 | 15 |
|
| 16 | +```shell |
| 17 | +poetry new spawn-py-demo |
7 | 18 | ``` |
8 | | -> git clone https://github.com/eigr-labs/spawn-python-sdk.git |
9 | | -Cloning into 'spawn-python-sdk'... |
10 | 19 |
|
11 | | -> cd spawn-python-sdk |
12 | | -> python3 -m venv env |
13 | | -> source ./env/bin/activate |
14 | | -> python --version |
15 | | -Python 3.7.3 |
16 | | -> pip --version |
17 | | -> pip install wheel |
18 | | -> pip install . |
| 20 | +The second thing we have to do is add the spawn dependency to the project. |
| 21 | + |
| 22 | +```toml |
| 23 | +[tool.poetry.dependencies] |
| 24 | +python = ">=3.9.7,<4.0" |
| 25 | +spawn = {git = "https://github.com/eigr/spawn-python-sdk.git"} |
19 | 26 | ``` |
20 | 27 |
|
21 | | -### Generate installer |
| 28 | +Now it is necessary to download the dependencies via Poetry: |
| 29 | + |
| 30 | +```shell |
| 31 | +poetry env use python3 # use your own python here |
| 32 | +poetry lock |
| 33 | +poetry install |
| 34 | +``` |
| 35 | + |
| 36 | +So far it's all pretty boring and not really Spawn related so it's time to start playing for real. |
| 37 | +The first thing we're going to do is define a place to put our protobuf files. In the root of the project we will create a folder called protobuf and some subfolders |
| 38 | + |
| 39 | +```shell |
| 40 | +mkdir -p spawn_py_demo/protobuf/domain |
22 | 41 | ``` |
23 | | -python setup.py bdist_wheel |
| 42 | + |
| 43 | +That done, let's create our protobuf file inside the example folder. |
| 44 | + |
| 45 | +```shell |
| 46 | +touch spawn_py_demo/protobuf/domain/domain.proto |
| 47 | +``` |
| 48 | + |
| 49 | +And let's populate this file with the following content: |
| 50 | + |
| 51 | +```proto |
| 52 | +syntax = "proto3"; |
| 53 | +
|
| 54 | +package domain; |
| 55 | +
|
| 56 | +message JoeState { |
| 57 | + repeated string languages = 1; |
| 58 | +} |
| 59 | +
|
| 60 | +message Request { |
| 61 | + string language = 1; |
| 62 | +} |
| 63 | +
|
| 64 | +message Reply { |
| 65 | + string response = 1; |
| 66 | +} |
24 | 67 | ``` |
25 | 68 |
|
26 | | -### Local install |
| 69 | +We must compile this file using the protoc utility. In the root of the project type the following command: |
| 70 | + |
| 71 | +```shell |
| 72 | +protoc -I spawn_py_demo/protobuf/ --python_out=spawn_py_demo spawn_py_demo/protobuf/domain/domain.proto |
| 73 | +``` |
| 74 | + |
| 75 | +Now in the spawn_py_demo folder we will create our first python file containing the code of our Actor. |
| 76 | + |
| 77 | +```shell |
| 78 | +touch spawn_py_demo/joe.py |
| 79 | +``` |
| 80 | + |
| 81 | +Populate this file with the following content: |
| 82 | + |
| 83 | +```python |
| 84 | +from domain.domain_pb2 import JoeState, Request, Reply |
| 85 | +from spawn.eigr.functions.actors.api.actor import Actor |
| 86 | +from spawn.eigr.functions.actors.api.settings import ActorSettings |
| 87 | +from spawn.eigr.functions.actors.api.context import Context |
| 88 | +from spawn.eigr.functions.actors.api.value import Value |
| 89 | +from spawn.eigr.functions.actors.api.workflows.broadcast import Broadcast |
| 90 | + |
| 91 | +actor = Actor(settings=ActorSettings( |
| 92 | + name="joe", stateful=True, channel="test")) |
| 93 | + |
| 94 | + |
| 95 | +@actor.timer_action(every=1000) |
| 96 | +def hi(ctx: Context) -> Value: |
| 97 | + new_state = None |
| 98 | + request = Request() |
| 99 | + request.language = "python" |
| 100 | + broadcast = Broadcast() |
| 101 | + broadcast.channel = "test" |
| 102 | + broadcast.action_name = "setLanguage" |
| 103 | + broadcast.value = request |
| 104 | + |
| 105 | + if not ctx.state: |
| 106 | + new_state = JoeState() |
| 107 | + new_state.languages.append("python") |
| 108 | + else: |
| 109 | + new_state = ctx.state |
| 110 | + |
| 111 | + return Value()\ |
| 112 | + .broadcast(broadcast)\ |
| 113 | + .state(new_state)\ |
| 114 | + .noreply() |
| 115 | + |
| 116 | + |
| 117 | +@actor.action("setLanguage") |
| 118 | +def set_language(request: Request, ctx: Context) -> Value: |
| 119 | + reply = Reply() |
| 120 | + reply.response = "erlang" |
| 121 | + return Value().of(reply, ctx.state).reply() |
| 122 | +``` |
| 123 | + |
| 124 | +Now with our Actor properly defined, we just need to start the SDK correctly. Create another file to serve as your application's entrypoint and fill it with the following content: |
| 125 | + |
| 126 | +```python |
| 127 | +from spawn.eigr.functions.actors.api.sdk import Spawn |
| 128 | +from joe import actor as joe_actor |
| 129 | + |
| 130 | +if __name__ == "__main__": |
| 131 | + spawn = Spawn() |
| 132 | + spawn.port(8091).proxy_port(9003).actor_system( |
| 133 | + "spawn-system").add_actor(joe_actor).start() |
27 | 134 | ``` |
28 | | -python -m pip install dist/spawn-python-sdk-0.1.0-py3-none-any.whl |
29 | | -``` |
|
0 commit comments