Skip to content

Commit bfb4b59

Browse files
committed
Restructure the Go, Python, and TypeScript Worker pages
Gives all three pages the same six sections, ordered by the reader's journey: create and run, register types, connect to Cloud, configure options, run a versioned Worker, shut down. - Adds a versioned Worker section. None of the eight SDK Worker pages documented Worker Versioning. - Replaces hand-written inline code with snipsync snippets. - Un-nests the TypeScript register-types section, which was an H3 inside the Temporal Cloud section. - Replaces the TypeScript mTLS-only Cloud walkthrough with a pointer to the Client page, which also covers API keys. - Fills out the Python page, previously a single section. Existing anchor IDs are preserved because eight other pages link to them. The TypeScript Docker section is unchanged, moved after the core sections.
1 parent a6dfee7 commit bfb4b59

3 files changed

Lines changed: 389 additions & 247 deletions

File tree

docs/develop/go/workers/run-worker-process.mdx

Lines changed: 66 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -22,36 +22,44 @@ Create a [`Worker`](https://pkg.go.dev/go.temporal.io/sdk/worker#Worker) by call
2222
2. The name of the Task Queue to poll.
2323
3. A [`worker.Options`](https://pkg.go.dev/go.temporal.io/sdk/internal#WorkerOptions) struct (can be empty for defaults).
2424

25-
Register your Workflow and Activity types, then call `Run()` to start polling. The Worker process is a long-running process that blocks while polling for tasks.
26-
Run it in a separate terminal from your starter code or other application logic.
25+
Register your Workflow and Activity types, then call `Run()` to start polling.
26+
The Worker blocks while it polls, so run it in a separate terminal from your starter code.
2727

28+
<!--SNIPSTART go-create-worker-->
29+
[helloworld/worker/main.go](https://github.com/temporalio/samples-go/blob/main/helloworld/worker/main.go)
2830
```go
2931
package main
3032

3133
import (
32-
"log"
34+
"log"
3335

34-
"go.temporal.io/sdk/client"
35-
"go.temporal.io/sdk/worker"
36+
"go.temporal.io/sdk/client"
37+
"go.temporal.io/sdk/contrib/envconfig"
38+
"go.temporal.io/sdk/worker"
39+
40+
"github.com/temporalio/samples-go/helloworld"
3641
)
3742

3843
func main() {
39-
c, err := client.Dial(client.Options{})
40-
if err != nil {
41-
log.Fatalln("Unable to create client", err)
42-
}
43-
defer c.Close()
44-
45-
w := worker.New(c, "my-task-queue", worker.Options{})
46-
w.RegisterWorkflow(MyWorkflow)
47-
w.RegisterActivity(MyActivity)
48-
49-
err = w.Run(worker.InterruptCh())
50-
if err != nil {
51-
log.Fatalln("Unable to start Worker", err)
52-
}
44+
// The client and worker are heavyweight objects that should be created once per process.
45+
c, err := client.Dial(envconfig.MustLoadDefaultClientOptions())
46+
if err != nil {
47+
log.Fatalln("Unable to create client", err)
48+
}
49+
defer c.Close()
50+
51+
w := worker.New(c, "hello-world", worker.Options{})
52+
53+
w.RegisterWorkflow(helloworld.Workflow)
54+
w.RegisterActivity(helloworld.Activity)
55+
56+
err = w.Run(worker.InterruptCh())
57+
if err != nil {
58+
log.Fatalln("Unable to start worker", err)
59+
}
5360
}
5461
```
62+
<!--SNIPEND-->
5563

5664
`Run()` accepts an interrupt channel so the Worker shuts down on `SIGINT` or `SIGTERM`.
5765
You can also call `Start()` and `Stop()` separately for more control over the lifecycle.
@@ -67,11 +75,6 @@ gow run worker/main.go
6775

6876
:::
6977

70-
## Connect to Temporal Cloud {/* #connect-to-temporal-cloud */}
71-
72-
To run a Worker against Temporal Cloud, configure the client connection with your Namespace address and authentication credentials.
73-
See [Connect to Temporal Cloud](/develop/go/client/temporal-client#connect-to-temporal-cloud) for setup instructions.
74-
7578
## Register Workflows and Activities {/* #register-types */}
7679

7780
All Workers listening to the same Task Queue must be registered to handle the same Workflow Types and Activity Types.
@@ -89,9 +92,46 @@ w.RegisterActivity(&MyActivities{})
8992
To customize the registered name or other options, use `RegisterWorkflowWithOptions()` or `RegisterActivityWithOptions()`.
9093
See [`workflow.RegisterOptions`](https://pkg.go.dev/go.temporal.io/sdk/workflow#RegisterOptions) and [`activity.RegisterOptions`](https://pkg.go.dev/go.temporal.io/sdk/activity#RegisterOptions).
9194

92-
## Worker options {/* #worker-options */}
95+
## Connect to Temporal Cloud {/* #connect-to-temporal-cloud */}
96+
97+
To run a Worker against Temporal Cloud, configure the Client connection with your Namespace address and authentication credentials.
98+
See [Connect to Temporal Cloud](/develop/go/client/temporal-client#connect-to-temporal-cloud) for setup instructions.
99+
100+
## Configure Worker options {/* #worker-options */}
93101

94102
Pass a [`worker.Options`](https://pkg.go.dev/go.temporal.io/sdk/internal#WorkerOptions) struct to `worker.New()` to configure concurrency limits, pollers, timeouts, and other Worker behavior.
95103
An empty struct uses defaults that work for most cases.
96104

97-
For the full list of options and their defaults, see the [Go SDK reference](https://pkg.go.dev/go.temporal.io/sdk@v1.42.0/internal#WorkerOptions).
105+
To tune these values against real load, see [Worker performance](/develop/worker-performance) and the [Worker tuning reference](/develop/worker-tuning-reference).
106+
107+
## Run a versioned Worker {/* #versioned-worker */}
108+
109+
Set a Worker Deployment Version and enable versioning in `worker.Options`, then set a versioning behavior on each Workflow.
110+
111+
<!--SNIPSTART go-versioned-worker-->
112+
[features/snippets/worker/worker.go](https://github.com/temporalio/features/blob/main/features/snippets/worker/worker.go)
113+
```go
114+
w := worker.New(c, "my-task-queue", worker.Options{
115+
DeploymentOptions: worker.DeploymentOptions{
116+
UseVersioning: true,
117+
Version: worker.WorkerDeploymentVersion{
118+
DeploymentName: "my-app",
119+
BuildID: "1.0",
120+
},
121+
},
122+
})
123+
124+
w.RegisterWorkflowWithOptions(HelloWorkflow, workflow.RegisterOptions{
125+
VersioningBehavior: workflow.VersioningBehaviorPinned,
126+
})
127+
```
128+
<!--SNIPEND-->
129+
130+
See [Worker Versioning](/worker-versioning) for the available versioning behaviors and how new versions roll out.
131+
132+
## Shut down a Worker {/* #shut-down-a-worker */}
133+
134+
A Worker started with `Run(worker.InterruptCh())` shuts down when the process receives `SIGINT` or `SIGTERM`.
135+
It stops polling for new Tasks and waits for in-flight Tasks to finish, up to the `WorkerStopTimeout` set in `worker.Options`.
136+
137+
See [Worker shutdown](/encyclopedia/workers/worker-shutdown) for what happens to in-flight Workflow Tasks and Activities.
Lines changed: 90 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
---
22
id: run-worker-process
3-
title: Worker processes - Python SDK
4-
description: Shows how to run Worker processes with the Python SDK
5-
sidebar_label: Worker processes
3+
title: Run a Worker - Python SDK
4+
description: Create and run a Temporal Worker using the Python SDK.
5+
sidebar_label: Run a Worker
66
slug: /develop/python/workers/run-worker-process
77
toc_max_heading_level: 3
88
tags:
@@ -11,81 +11,110 @@ tags:
1111
- Worker
1212
---
1313

14-
import { ViewSourceCodeNotice } from '@site/src/components';
14+
This page covers long-lived Workers that you host and run as persistent processes.
15+
For Workers that run on serverless compute like AWS Lambda, see [Serverless Workers](/develop/python/workers/serverless-workers).
1516

16-
## Run a Worker Process {/* #run-a-dev-worker */}
17+
## Create and run a Worker {/* #run-a-dev-worker */}
1718

18-
**How to run a Worker Process using the Temporal Python SDK.**
19+
Create a `Worker` with a Temporal Client, the Task Queue to poll, and the Workflows and Activities it can execute.
20+
Call `run()` to start polling. The Worker runs until the process is interrupted.
1921

20-
The [Worker Process](/workers#worker-process) is where Workflow Functions and Activity Functions are executed.
22+
<!--SNIPSTART python-create-worker-->
23+
[features/snippets/worker/worker.py](https://github.com/temporalio/features/blob/main/features/snippets/worker/worker.py)
24+
```py
25+
client = await Client.connect("localhost:7233")
2126

22-
- Each [Worker Entity](/workers#worker-entity) in the Worker Process must register the exact Workflow Types and Activity
23-
Types it may execute.
24-
- Each Worker Entity must also associate itself with exactly one [Task Queue](/task-queue).
25-
- Each Worker Entity polling the same Task Queue must be registered with the same Workflow Types and Activity Types.
27+
worker = Worker(
28+
client,
29+
task_queue="my-task-queue",
30+
workflows=[HelloWorkflow],
31+
activities=[some_activity],
32+
)
33+
await worker.run()
34+
```
35+
<!--SNIPEND-->
2636

27-
A [Worker Entity](/workers#worker-entity) is the component within a Worker Process that listens to a specific Task
28-
Queue.
37+
A Worker is also an async context manager, so `async with worker:` runs it for the duration of a block.
38+
See [Shut down a Worker](#shut-down-a-worker) for the pattern most Worker processes use.
2939

30-
Although multiple Worker Entities can be in a single Worker Process, a single Worker Entity Worker Process may be
31-
perfectly sufficient. For more information, see the [Worker tuning guide](/develop/worker-performance).
40+
## Register Workflows and Activities {/* #register-types */}
3241

33-
A Worker Entity contains a Workflow Worker and/or an Activity Worker, which makes progress on Workflow Executions and
34-
Activity Executions, respectively.
42+
All Workers listening to the same Task Queue must be registered to handle the same Workflow Types and Activity Types.
43+
If a Worker polls a Task for a type it does not know about, the Task fails. The Workflow Execution itself does not fail.
3544

36-
To develop a Worker, use the `Worker()` constructor and add your Client, Task Queue, Workflows, and Activities as
37-
arguments. The following code example creates a Worker that polls for tasks from the Task Queue and executes the
38-
Workflow. When a Worker is created, it accepts a list of Workflows in the workflows parameter, a list of Activities in
39-
the activities parameter, or both.
45+
Pass a list of Workflows in `workflows`, a list of Activities in `activities`, or both.
4046

41-
<ViewSourceCodeNotice href="https://github.com/temporalio/documentation/blob/main/sample-apps/python/your_app/run_worker_dacx.py" />
47+
Activities defined with `async def` run on the Worker's event loop. Activities defined with a plain `def` are synchronous and require an executor, so pass one in `activity_executor`:
4248

4349
```python
44-
from temporalio.client import Client
45-
from temporalio.worker import Worker
46-
# ...
47-
# ...
48-
async def main():
49-
client = await Client.connect("localhost:7233")
50-
worker = Worker(
51-
client,
52-
task_queue="your-task-queue",
53-
workflows=[YourWorkflow],
54-
activities=[your_activity],
55-
)
56-
await worker.run()
57-
58-
if __name__ == "__main__":
59-
asyncio.run(main())
50+
worker = Worker(
51+
client,
52+
task_queue="my-task-queue",
53+
workflows=[MyWorkflow],
54+
activities=[my_sync_activity],
55+
activity_executor=ThreadPoolExecutor(5),
56+
)
6057
```
6158

62-
### Register types {/* #register-types */}
59+
The same executor can be shared across multiple Workers.
6360

64-
**How to register types using the Temporal Python SDK.**
61+
## Connect to Temporal Cloud {/* #connect-to-temporal-cloud */}
6562

66-
All Workers listening to the same Task Queue name must be registered to handle the exact same Workflows Types and
67-
Activity Types.
63+
To run a Worker against Temporal Cloud, configure the Client connection with your Namespace address and authentication credentials.
64+
See [Connect to Temporal Cloud](/develop/python/client/temporal-client#connect-to-temporal-cloud) for setup instructions.
6865

69-
If a Worker polls a Task for a Workflow Type or Activity Type it does not know about, it fails that Task. However, the
70-
failure of the Task does not cause the associated Workflow Execution to fail.
66+
## Configure Worker options {/* #worker-options */}
7167

72-
When a `Worker` is created, it accepts a list of Workflows in the `workflows` parameter, a list of Activities in the
73-
`activities` parameter, or both.
68+
The `Worker` constructor takes keyword arguments that control concurrency limits, pollers, timeouts, and caching, including `max_concurrent_activities`, `max_concurrent_workflow_tasks`, and `max_cached_workflows`.
69+
The defaults work for most cases.
7470

75-
<ViewSourceCodeNotice href="https://github.com/temporalio/documentation/blob/main/sample-apps/python/your_app/run_worker_dacx.py" />
71+
To tune these values against real load, see [Worker performance](/develop/worker-performance) and the [Worker tuning reference](/develop/worker-tuning-reference).
7672

77-
```python
78-
# ...
79-
async def main():
80-
client = await Client.connect("localhost:7233")
81-
worker = Worker(
82-
client,
83-
task_queue="your-task-queue",
84-
workflows=[YourWorkflow],
85-
activities=[your_activity],
86-
)
87-
await worker.run()
88-
89-
if __name__ == "__main__":
90-
asyncio.run(main())
73+
## Run a versioned Worker {/* #versioned-worker */}
74+
75+
Set a Worker Deployment Version and enable versioning in `deployment_config`, then set a default versioning behavior for the Workflows on the Worker.
76+
77+
<!--SNIPSTART python-versioned-worker-->
78+
[features/snippets/worker/worker.py](https://github.com/temporalio/features/blob/main/features/snippets/worker/worker.py)
79+
```py
80+
worker = Worker(
81+
client,
82+
task_queue="my-task-queue",
83+
workflows=[HelloWorkflow],
84+
activities=[some_activity],
85+
deployment_config=WorkerDeploymentConfig(
86+
version=WorkerDeploymentVersion(
87+
deployment_name="my-app",
88+
build_id="1.0",
89+
),
90+
use_worker_versioning=True,
91+
default_versioning_behavior=VersioningBehavior.PINNED,
92+
),
93+
)
94+
```
95+
<!--SNIPEND-->
96+
97+
To set the behavior per Workflow instead of on the Worker, pass `versioning_behavior` to `@workflow.defn`.
98+
See [Worker Versioning](/worker-versioning) for the available versioning behaviors and how new versions roll out.
99+
100+
## Shut down a Worker {/* #shut-down-a-worker */}
101+
102+
Use the Worker as an async context manager and wait on an event that your signal handler sets.
103+
When the block exits, the Worker stops polling for new Tasks and waits for in-flight Tasks to finish, up to `graceful_shutdown_timeout`.
104+
105+
<!--SNIPSTART python-worker-graceful-shutdown-->
106+
[features/snippets/worker/worker.py](https://github.com/temporalio/features/blob/main/features/snippets/worker/worker.py)
107+
```py
108+
worker = Worker(
109+
client,
110+
task_queue="my-task-queue",
111+
workflows=[HelloWorkflow],
112+
activities=[some_activity],
113+
graceful_shutdown_timeout=timedelta(seconds=30),
114+
)
115+
async with worker:
116+
await interrupt_event.wait()
91117
```
118+
<!--SNIPEND-->
119+
120+
See [Worker shutdown](/encyclopedia/workers/worker-shutdown) for what happens to in-flight Workflow Tasks and Activities.

0 commit comments

Comments
 (0)