Overview
TheWorker class manages Temporal workflow workers, handling the execution of workflows and activities with support for graceful shutdown, concurrent execution limits, and custom module sandboxing.
Workers poll Temporal for tasks and execute workflow and activity code. In production, you typically run workers separately from API servers for better scalability.
Key Features
- Graceful Shutdown: Responds to SIGTERM/SIGINT signals, allowing in-flight activities to complete
- Concurrency Control: Configurable maximum concurrent activities
- Custom Executors: Support for custom thread pool executors
- Module Sandboxing: Control which modules are available in workflow execution
- Daemon Mode: Run workers in background threads for local development
Initialization
Constructor Parameters
Client for interacting with Temporal. Provides connection details and task queue configuration.
List of activity functions that this worker can execute.
List of workflow classes that this worker can execute.
Additional Python modules to make available in the workflow sandbox.Default modules always available:
["application_sdk", "pandas", "os", "app"]Maximum number of activities that can run concurrently.Set to
None for no limit. Configurable via MAX_CONCURRENT_ACTIVITIES environment variable.Custom thread pool executor for running activities.If not provided, a default
ThreadPoolExecutor with max_concurrent_activities workers is created.Starting the Worker
Production Mode
Run the worker in the foreground (non-daemon mode). This is the standard mode for production worker pods.Development Mode
Run the worker in daemon mode for local development. This starts the worker in a background thread.start() Method Parameters
Whether to run the worker in daemon mode (background thread).
True: Worker runs in background thread (local development)False: Worker runs in foreground (production)
Graceful Shutdown
Workers support graceful shutdown to ensure in-flight activities complete before termination.How It Works
- Worker receives SIGTERM or SIGINT signal
- Worker stops polling for new tasks
- In-flight activities continue executing
- Worker waits for activities to complete (up to
GRACEFUL_SHUTDOWN_TIMEOUT_SECONDS) - Worker exits when all activities finish or timeout is reached
Graceful shutdown timeout is configurable via the
GRACEFUL_SHUTDOWN_TIMEOUT_SECONDS environment variable (default: 30 seconds).Platform Support
Unix/Linux/macOS
Unix/Linux/macOS
Full signal handling support via asyncio event loop.Workers respond to:
SIGTERM: Graceful shutdownSIGINT: Graceful shutdown (Ctrl+C)
Windows
Windows
Signal handling is not supported on Windows.
Complete Worker Example
Worker with Application
Typically, workers are managed by theBaseApplication class:
Environment Variables
Workers respect these environment variables:| Variable | Description | Default |
|---|---|---|
APPLICATION_MODE | Deployment mode (LOCAL, WORKER, SERVER) | LOCAL |
MAX_CONCURRENT_ACTIVITIES | Max concurrent activities | System default |
GRACEFUL_SHUTDOWN_TIMEOUT_SECONDS | Shutdown timeout in seconds | 30 |
DEPLOYMENT_NAME | Deployment identifier | - |
Event Loop Optimization
The worker automatically selects the best event loop implementation:Unix/Linux/macOS
Usesuvloop for better performance (if installed):
Windows
UsesWindowsSelectorEventLoopPolicy for compatibility.
Worker Lifecycle
Best Practices
Set Appropriate Concurrency Limits
Set Appropriate Concurrency Limits
Configure
max_concurrent_activities based on your resource constraints.Use Dedicated Workers for Production
Use Dedicated Workers for Production
Run workers in separate pods/containers from API servers.
Include Required Modules in Passthrough
Include Required Modules in Passthrough
Add all modules your workflows and activities need.
Monitor Worker Health
Monitor Worker Health
Implement health checks and monitoring for production workers.
Handle Graceful Shutdown Properly
Handle Graceful Shutdown Properly
Ensure activities can complete within the shutdown timeout.
Instance Attributes
| Attribute | Type | Description |
|---|---|---|
workflow_client | Optional[WorkflowClient] | Client for Temporal communication |
workflow_worker | Optional[TemporalWorker] | Temporal worker instance |
workflow_activities | Sequence[CallableType] | Registered activity functions |
workflow_classes | Sequence[ClassType] | Registered workflow classes |
passthrough_modules | List[str] | Modules available in workflow sandbox |
max_concurrent_activities | Optional[int] | Max concurrent activity executions |
activity_executor | ThreadPoolExecutor | Executor for running activities |
Related Components
- Application - Manages worker lifecycle
- Workflows - Executed by workers
- Activities - Task implementations run by workers
- Server - Triggers workflows that workers execute