|
| 1 | +# Standalone Activity |
| 2 | + |
| 3 | +This sample shows how to execute Activities directly from a Temporal Client, without a Workflow. |
| 4 | + |
| 5 | +For full documentation, see [Standalone Activities - Python SDK](https://docs.temporal.io/develop/python/standalone-activities). |
| 6 | + |
| 7 | +### Sample directory structure |
| 8 | + |
| 9 | +- [my_activity.py](./my_activity.py) - Activity definition with `@activity.defn` |
| 10 | +- [worker.py](./worker.py) - Worker that registers and runs the Activity |
| 11 | +- [execute_activity.py](./execute_activity.py) - Execute a Standalone Activity and wait for the result |
| 12 | +- [start_activity.py](./start_activity.py) - Start a Standalone Activity, get a handle, then wait for the result |
| 13 | +- [list_activities.py](./list_activities.py) - List Standalone Activity Executions |
| 14 | +- [count_activities.py](./count_activities.py) - Count Standalone Activity Executions |
| 15 | + |
| 16 | +### Quickstart |
| 17 | + |
| 18 | +**1. Start the Temporal dev server** |
| 19 | + |
| 20 | +```bash |
| 21 | +temporal server start-dev |
| 22 | +``` |
| 23 | + |
| 24 | +**2. Run the Worker** (in a separate terminal) |
| 25 | + |
| 26 | +```bash |
| 27 | +uv run hello_standalone_activity/worker.py |
| 28 | +``` |
| 29 | + |
| 30 | +**3. Execute a Standalone Activity** (in a separate terminal) |
| 31 | + |
| 32 | +Execute and wait for the result: |
| 33 | + |
| 34 | +```bash |
| 35 | +uv run hello_standalone_activity/execute_activity.py |
| 36 | +``` |
| 37 | + |
| 38 | +Or use the Temporal CLI: |
| 39 | + |
| 40 | +```bash |
| 41 | +temporal activity execute \ |
| 42 | + --type compose_greeting \ |
| 43 | + --activity-id my-standalone-activity-id \ |
| 44 | + --task-queue my-standalone-activity-task-queue \ |
| 45 | + --start-to-close-timeout 10s \ |
| 46 | + --input '{"greeting": "Hello", "name": "World"}' |
| 47 | +``` |
| 48 | + |
| 49 | +**4. Start a Standalone Activity (without waiting)** |
| 50 | + |
| 51 | +Start, get a handle, then wait for the result: |
| 52 | + |
| 53 | +```bash |
| 54 | +uv run hello_standalone_activity/start_activity.py |
| 55 | +``` |
| 56 | + |
| 57 | +Or use the Temporal CLI: |
| 58 | + |
| 59 | +```bash |
| 60 | +temporal activity start \ |
| 61 | + --type compose_greeting \ |
| 62 | + --activity-id my-standalone-activity-id \ |
| 63 | + --task-queue my-standalone-activity-task-queue \ |
| 64 | + --start-to-close-timeout 10s \ |
| 65 | + --input '{"greeting": "Hello", "name": "World"}' |
| 66 | +``` |
| 67 | + |
| 68 | +**5. List Standalone Activities** |
| 69 | + |
| 70 | +```bash |
| 71 | +uv run hello_standalone_activity/list_activities.py |
| 72 | +``` |
| 73 | + |
| 74 | +Or use the Temporal CLI: |
| 75 | + |
| 76 | +```bash |
| 77 | +temporal activity list --query "TaskQueue = 'my-standalone-activity-task-queue'" |
| 78 | +``` |
| 79 | + |
| 80 | +Note: `list` and `count` are only available in the [Standalone Activity prerelease CLI](https://github.com/temporalio/cli/releases/tag/v1.6.2-standalone-activity). |
| 81 | + |
| 82 | +**6. Count Standalone Activities** |
| 83 | + |
| 84 | +```bash |
| 85 | +uv run hello_standalone_activity/count_activities.py |
| 86 | +``` |
| 87 | + |
| 88 | +Or use the Temporal CLI: |
| 89 | + |
| 90 | +```bash |
| 91 | +temporal activity count --query "TaskQueue = 'my-standalone-activity-task-queue'" |
| 92 | +``` |
| 93 | + |
| 94 | +### Temporal Cloud |
| 95 | + |
| 96 | +The same code works against Temporal Cloud - just set environment variables. No code changes needed. |
| 97 | + |
| 98 | +**Connect with mTLS:** |
| 99 | + |
| 100 | +```bash |
| 101 | +export TEMPORAL_ADDRESS=<your-namespace>.<your-account-id>.tmprl.cloud:7233 |
| 102 | +export TEMPORAL_NAMESPACE=<your-namespace>.<your-account-id> |
| 103 | +export TEMPORAL_TLS_CLIENT_CERT_PATH='path/to/your/client.pem' |
| 104 | +export TEMPORAL_TLS_CLIENT_KEY_PATH='path/to/your/client.key' |
| 105 | +``` |
| 106 | + |
| 107 | +**Connect with an API key:** |
| 108 | + |
| 109 | +```bash |
| 110 | +export TEMPORAL_ADDRESS=<region>.<cloud_provider>.api.temporal.io:7233 |
| 111 | +export TEMPORAL_NAMESPACE=<your-namespace>.<your-account-id> |
| 112 | +export TEMPORAL_API_KEY=<your-api-key> |
| 113 | +``` |
| 114 | + |
| 115 | +Then run the worker and starter as shown above. |
0 commit comments