Skip to content

Commit a5b67e4

Browse files
authored
Update SDK and add hello world sample (temporalio#3)
1 parent 4a4f1f4 commit a5b67e4

File tree

7 files changed

+300
-210
lines changed

7 files changed

+300
-210
lines changed

.github/workflows/ci.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,6 @@ jobs:
2727
- run: python -m pip install --upgrade wheel poetry poethepoet
2828
- run: poetry install
2929
- run: poe lint
30-
- run: poe test -s -o log_cli_level=DEBUG
30+
# TODO: Re-enable when tests appear
31+
# - run: poe test -s -o log_cli_level=DEBUG
3132

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,19 @@ Prerequisites:
1212

1313
* Python >= 3.7
1414
* [Poetry](https://python-poetry.org)
15+
* [Local Temporal server running](https://docs.temporal.io/clusters/quick-install/)
1516

1617
With this repository cloned, run the following at the root of the directory:
1718

1819
poetry install --no-root
1920

20-
That loads all dependencies. Then to run a sample, usually you just run it in Python:
21+
That loads all dependencies. Then to run a sample, usually you just run it in Python. For example:
2122

22-
poetry run python script_name.py
23+
poetry run python hello_world/hello_world.py
2324

2425
See each sample's directory for specific instructions.
2526

2627
## Samples
2728

2829
* [Activity Worker](activity_worker) - Use Python activities from a workflow in another language
30+
* [Hello World](hello_world) - Basic hello world workflow and activity

activity_worker/activity_worker.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import random
33
import string
44

5+
from temporalio import activity
56
from temporalio.client import Client
67
from temporalio.worker import Worker
78

@@ -10,6 +11,7 @@
1011
activity_name = "say-hello-activity"
1112

1213

14+
@activity.defn(name=activity_name)
1315
async def say_hello_activity(name: str) -> str:
1416
return f"Hello, {name}!"
1517

@@ -19,9 +21,7 @@ async def main():
1921
client = await Client.connect("http://localhost:7233")
2022

2123
# Run activity worker
22-
async with Worker(
23-
client, task_queue=task_queue, activities={activity_name: say_hello_activity}
24-
):
24+
async with Worker(client, task_queue=task_queue, activities=[say_hello_activity]):
2525
# Run the Go workflow
2626
workflow_id = "".join(
2727
random.choices(string.ascii_uppercase + string.digits, k=30)

hello_world/README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Hello World
2+
3+
This sample shows a basic workflow that calls a basic activity, a worker that runs the workflow/activity, and a client
4+
that invokes the workflow.
5+
6+
To run, first see [README.md](../README.md) for prerequisites. Then, run the following from this directory:
7+
8+
poetry run python hello_world.py
9+
10+
The result will be:
11+
12+
Result: Hello, Temporal!

hello_world/hello_world.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import asyncio
2+
import logging
3+
from datetime import timedelta
4+
5+
from temporalio import activity, workflow
6+
from temporalio.client import Client
7+
from temporalio.worker import Worker
8+
9+
10+
# Basic activity that logs and does string concatenation
11+
@activity.defn
12+
async def say_hello_activity(name: str) -> str:
13+
activity.logger.info("Running activity with parameter %s" % name)
14+
return f"Hello, {name}!"
15+
16+
17+
# Basic workflow that logs and invokes an activity
18+
@workflow.defn
19+
class SayHelloWorkflow:
20+
@workflow.run
21+
async def run(self, name: str) -> str:
22+
workflow.logger.info("Running workflow with parameter %s" % name)
23+
return await workflow.execute_activity(
24+
say_hello_activity, name, start_to_close_timeout=timedelta(seconds=10)
25+
)
26+
27+
28+
async def main():
29+
# Uncomment the line below to see logging
30+
# logging.basicConfig(level=logging.INFO)
31+
32+
# Start client
33+
client = await Client.connect("http://localhost:7233")
34+
35+
# Run a worker for the workflow
36+
async with Worker(
37+
client,
38+
task_queue="my-task-queue",
39+
workflows=[SayHelloWorkflow],
40+
activities=[say_hello_activity],
41+
):
42+
43+
# While the worker is running, use the client to run the workflow and
44+
# print out its result. Note, in many production setups, the client
45+
# would be in a completely separate process from the worker.
46+
result = await client.execute_workflow(
47+
SayHelloWorkflow.run,
48+
"Temporal",
49+
id="my-workflow-id",
50+
task_queue="my-task-queue",
51+
)
52+
print(f"Result: {result}")
53+
54+
55+
if __name__ == "__main__":
56+
asyncio.run(main())

0 commit comments

Comments
 (0)