Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,6 @@ jobs:
- run: python -m pip install --upgrade wheel poetry poethepoet
- run: poetry install
- run: poe lint
- run: poe test -s -o log_cli_level=DEBUG
# TODO: Re-enable when tests appear
# - run: poe test -s -o log_cli_level=DEBUG

6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,19 @@ Prerequisites:

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

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

poetry install --no-root

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

poetry run python script_name.py
poetry run python hello_world/hello_world.py

See each sample's directory for specific instructions.

## Samples

* [Activity Worker](activity_worker) - Use Python activities from a workflow in another language

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd just remove this no need for it anymore

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it has some polyglot value, but later as the sample count grows, I may rename it to "activity-only worker" or something.

* [Hello World](hello_world) - Basic hello world workflow and activity
6 changes: 3 additions & 3 deletions activity_worker/activity_worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import random
import string

from temporalio import activity
from temporalio.client import Client
from temporalio.worker import Worker

Expand All @@ -10,6 +11,7 @@
activity_name = "say-hello-activity"


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

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

# Run activity worker
async with Worker(
client, task_queue=task_queue, activities={activity_name: say_hello_activity}
):
async with Worker(client, task_queue=task_queue, activities=[say_hello_activity]):
# Run the Go workflow
workflow_id = "".join(
random.choices(string.ascii_uppercase + string.digits, k=30)
Expand Down
12 changes: 12 additions & 0 deletions hello_world/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Hello World

This sample shows a basic workflow that calls a basic activity, a worker that runs the workflow/activity, and a client
that invokes the workflow.

To run, first see [README.md](../README.md) for prerequisites. Then, run the following from this directory:

poetry run python hello_world.py

The result will be:

Result: Hello, Temporal!
56 changes: 56 additions & 0 deletions hello_world/hello_world.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import asyncio
import logging
from datetime import timedelta

from temporalio import activity, workflow
from temporalio.client import Client
from temporalio.worker import Worker


# Basic activity that logs and does string concatenation
@activity.defn
async def say_hello_activity(name: str) -> str:
activity.logger.info("Running activity with parameter %s" % name)
return f"Hello, {name}!"


# Basic workflow that logs and invokes an activity
@workflow.defn
class SayHelloWorkflow:
@workflow.run
async def run(self, name: str) -> str:
workflow.logger.info("Running workflow with parameter %s" % name)
return await workflow.execute_activity(
say_hello_activity, name, start_to_close_timeout=timedelta(seconds=10)
)


async def main():
# Uncomment the line below to see logging
# logging.basicConfig(level=logging.INFO)

# Start client
client = await Client.connect("http://localhost:7233")

# Run a worker for the workflow
async with Worker(
client,
task_queue="my-task-queue",
workflows=[SayHelloWorkflow],
activities=[say_hello_activity],
):

# While the worker is running, use the client to run the workflow and
# print out its result. Note, in many production setups, the client
# would be in a completely separate process from the worker.
result = await client.execute_workflow(
SayHelloWorkflow.run,
"Temporal",
id="my-workflow-id",
task_queue="my-task-queue",
)
print(f"Result: {result}")


if __name__ == "__main__":
asyncio.run(main())
Loading