Skip to content

Commit 4a4f1f4

Browse files
authored
Initial activity worker example (#1)
1 parent 0d07706 commit 4a4f1f4

File tree

11 files changed

+1057
-0
lines changed

11 files changed

+1057
-0
lines changed

.github/workflows/ci.yml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
name: Continuous Integration
2+
on: # rebuild any PRs and main branch changes
3+
pull_request:
4+
push:
5+
branches:
6+
- main
7+
- "releases/*"
8+
9+
jobs:
10+
# Build and test the project
11+
build-lint-test:
12+
strategy:
13+
fail-fast: true
14+
matrix:
15+
python: ["3.7", "3.10"]
16+
os: [ubuntu-latest, macos-latest, windows-latest]
17+
runs-on: ${{ matrix.os }}
18+
steps:
19+
- name: Print build information
20+
run: "echo head_ref: ${{ github.head_ref }}, ref: ${{ github.ref }}, os: ${{ matrix.os }}, python: ${{ matrix.python }}"
21+
- uses: actions/checkout@v2
22+
with:
23+
submodules: recursive
24+
- uses: actions/setup-python@v1
25+
with:
26+
python-version: ${{ matrix.python }}
27+
- run: python -m pip install --upgrade wheel poetry poethepoet
28+
- run: poetry install
29+
- run: poe lint
30+
- run: poe test -s -o log_cli_level=DEBUG
31+

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.venv
2+
__pycache__

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License
2+
3+
Copyright (c) 2022 Temporal Technologies Inc. All rights reserved.
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in
13+
all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
THE SOFTWARE.

README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Temporal Python SDK Samples
2+
3+
This is the set of Python samples for the [Python SDK](https://github.com/temporalio/sdk-python).
4+
5+
**UNDER DEVELOPMENT**
6+
7+
The Python SDK is under development. There are no compatibility guarantees nor proper documentation pages at this time.
8+
9+
## Usage
10+
11+
Prerequisites:
12+
13+
* Python >= 3.7
14+
* [Poetry](https://python-poetry.org)
15+
16+
With this repository cloned, run the following at the root of the directory:
17+
18+
poetry install --no-root
19+
20+
That loads all dependencies. Then to run a sample, usually you just run it in Python:
21+
22+
poetry run python script_name.py
23+
24+
See each sample's directory for specific instructions.
25+
26+
## Samples
27+
28+
* [Activity Worker](activity_worker) - Use Python activities from a workflow in another language

activity_worker/README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Activity Worker
2+
3+
This sample shows a Go workflow calling a Python activity.
4+
5+
First run the Go workflow worker by running this in the `go_workflow` directory in a separate terminal:
6+
7+
go run .
8+
9+
Then in another terminal, run the sample from this directory:
10+
11+
poetry run python activity_worker.py
12+
13+
The Python code will invoke the Go workflow which will execute the Python activity and return.

activity_worker/activity_worker.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import asyncio
2+
import random
3+
import string
4+
5+
from temporalio.client import Client
6+
from temporalio.worker import Worker
7+
8+
task_queue = "say-hello-task-queue"
9+
workflow_name = "say-hello-workflow"
10+
activity_name = "say-hello-activity"
11+
12+
13+
async def say_hello_activity(name: str) -> str:
14+
return f"Hello, {name}!"
15+
16+
17+
async def main():
18+
# Create client to localhost on default namespace
19+
client = await Client.connect("http://localhost:7233")
20+
21+
# Run activity worker
22+
async with Worker(
23+
client, task_queue=task_queue, activities={activity_name: say_hello_activity}
24+
):
25+
# Run the Go workflow
26+
workflow_id = "".join(
27+
random.choices(string.ascii_uppercase + string.digits, k=30)
28+
)
29+
result = await client.execute_workflow(
30+
workflow_name, "Temporal", id=workflow_id, task_queue=task_queue
31+
)
32+
# Print out "Hello, Temporal!"
33+
print(result)
34+
35+
36+
if __name__ == "__main__":
37+
asyncio.run(main())

activity_worker/go_workflow/go.mod

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
module github.com/temporalio/samples-python/activity_worker/go_workflow
2+
3+
go 1.17
4+
5+
require go.temporal.io/sdk v1.14.0
6+
7+
require (
8+
github.com/davecgh/go-spew v1.1.1 // indirect
9+
github.com/facebookgo/clock v0.0.0-20150410010913-600d898af40a // indirect
10+
github.com/gogo/googleapis v1.4.1 // indirect
11+
github.com/gogo/protobuf v1.3.2 // indirect
12+
github.com/gogo/status v1.1.0 // indirect
13+
github.com/golang/mock v1.6.0 // indirect
14+
github.com/golang/protobuf v1.5.2 // indirect
15+
github.com/google/uuid v1.3.0 // indirect
16+
github.com/grpc-ecosystem/go-grpc-middleware v1.3.0 // indirect
17+
github.com/pborman/uuid v1.2.1 // indirect
18+
github.com/pmezard/go-difflib v1.0.0 // indirect
19+
github.com/robfig/cron v1.2.0 // indirect
20+
github.com/stretchr/objx v0.3.0 // indirect
21+
github.com/stretchr/testify v1.7.0 // indirect
22+
go.temporal.io/api v1.7.1-0.20220223032354-6e6fe738916a // indirect
23+
go.uber.org/atomic v1.9.0 // indirect
24+
golang.org/x/net v0.0.0-20220127200216-cd36cc0744dd // indirect
25+
golang.org/x/sys v0.0.0-20220222200937-f2425489ef4c // indirect
26+
golang.org/x/text v0.3.7 // indirect
27+
golang.org/x/time v0.0.0-20210723032227-1f47c861a9ac // indirect
28+
google.golang.org/genproto v0.0.0-20220222213610-43724f9ea8cf // indirect
29+
google.golang.org/grpc v1.44.0 // indirect
30+
google.golang.org/protobuf v1.27.1 // indirect
31+
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
32+
)

0 commit comments

Comments
 (0)