|
| 1 | +# Worker-Specific Task Queues |
| 2 | + |
| 3 | +Use a unique Task Queue for each Worker in order to have certain Activities run on a specific Worker. In the Go SDK, this is explicitly supported via the Session option, but in other SDKs a different approach is required. |
| 4 | + |
| 5 | +Typical use cases include tasks where interaction with a filesystem is required, such as data processing or interacting with legacy access structures. This example will write text files to folders corresponding to each worker, located in the `demo_fs` folder. In production, these folders would typically be independent machines in a worker cluster. |
| 6 | + |
| 7 | +This strategy is: |
| 8 | + |
| 9 | +- Each Worker process runs two `Worker`s: |
| 10 | + - One `Worker` listens on the `worker_specific_task_queue-distribution-queue` Task Queue. |
| 11 | + - Another `Worker` listens on a uniquely generated Task Queue. |
| 12 | +- The Workflow and the first Activity are run on `worker_specific_task_queue-distribution-queue`. |
| 13 | +- The first Activity returns one of the uniquely generated Task Queues (that only one Worker is listening on—i.e. the **Worker-specific Task Queue**). |
| 14 | +- The rest of the Activities do the file processing and are run on the Worker-specific Task Queue. |
| 15 | + |
| 16 | +Check the Temporal Web UI to confirm tasks were staying with their respective worker. |
| 17 | + |
| 18 | +It doesn't matter where the `get_available_task_queue` activity is run, so it can be executed on the shared Task Queue. In this demo, `unique_worker_task_queue` is simply a `uuid` initialized in the Worker, but you can inject smart logic here to uniquely identify the Worker, [as Netflix did](https://community.temporal.io/t/using-dynamic-task-queues-for-traffic-routing/3045). |
| 19 | + |
| 20 | +Activities have been artificially slowed with `time.sleep(3)` to simulate doing more work. |
| 21 | + |
| 22 | +### Running This Sample |
| 23 | + |
| 24 | +To run, first see [README.md](../README.md) for prerequisites. Then, run the following from this directory to start the |
| 25 | +worker: |
| 26 | + |
| 27 | + poetry run python worker.py |
| 28 | + |
| 29 | +This will start the worker. Then, in another terminal, run the following to execute the workflow: |
| 30 | + |
| 31 | + poetry run python starter.py |
| 32 | + |
| 33 | +#### Example output: |
| 34 | + |
| 35 | +```bash |
| 36 | +(temporalio-samples-py3.10) user@machine:~/samples-python/activities_sticky_queues$ poetry run python starter.py |
| 37 | +Output checksums: |
| 38 | +49d7419e6cba3575b3158f62d053f922aa08b23c64f05411cda3213b56c84ba4 |
| 39 | +49d7419e6cba3575b3158f62d053f922aa08b23c64f05411cda3213b56c84ba4 |
| 40 | +49d7419e6cba3575b3158f62d053f922aa08b23c64f05411cda3213b56c84ba4 |
| 41 | +49d7419e6cba3575b3158f62d053f922aa08b23c64f05411cda3213b56c84ba4 |
| 42 | +49d7419e6cba3575b3158f62d053f922aa08b23c64f05411cda3213b56c84ba4 |
| 43 | +49d7419e6cba3575b3158f62d053f922aa08b23c64f05411cda3213b56c84ba4 |
| 44 | +49d7419e6cba3575b3158f62d053f922aa08b23c64f05411cda3213b56c84ba4 |
| 45 | +49d7419e6cba3575b3158f62d053f922aa08b23c64f05411cda3213b56c84ba4 |
| 46 | +49d7419e6cba3575b3158f62d053f922aa08b23c64f05411cda3213b56c84ba4 |
| 47 | +49d7419e6cba3575b3158f62d053f922aa08b23c64f05411cda3213b56c84ba4 |
| 48 | +``` |
| 49 | + |
| 50 | +<details> |
| 51 | +<summary>Checking the history to see where activities are run</summary> |
| 52 | +All activities for the one workflow are running against the same task queue, which corresponds to unique workers: |
| 53 | + |
| 54 | + |
| 55 | + |
| 56 | +</details> |
0 commit comments