|
| 1 | +# coiled |
| 2 | + |
| 3 | +```{caution} |
| 4 | +Currently, the coiled backend can only be used if your workflow code is organized in a |
| 5 | +package due to how pytask imports your code and dask serializes task functions |
| 6 | +([issue](https://github.com/dask/distributed/issues/8607)). |
| 7 | +``` |
| 8 | + |
| 9 | +[coiled](https://www.coiled.io/) is a product built on top of dask that eases the |
| 10 | +deployment of your workflow to many cloud providers like AWS, GCP, and Azure. |
| 11 | + |
| 12 | +Note that, coiled is a paid service. They offer a |
| 13 | +[free monthly tier](https://www.coiled.io/pricing) where you only need to pay the costs |
| 14 | +for your cloud provider and you can get started without a credit card. |
| 15 | + |
| 16 | +They provide the following benefits which are especially helpful to people who are not |
| 17 | +familiar with cloud providers or remote computing. |
| 18 | + |
| 19 | +- coiled manages your resources by spawning workers if you need them and shutting them |
| 20 | + down if they are idle. |
| 21 | +- [Synchronization](https://docs.coiled.io/user_guide/software/sync.html) of your local |
| 22 | + environment to remote workers. |
| 23 | +- [Adaptive scaling](https://docs.dask.org/en/latest/adaptive.html) if your workflow |
| 24 | + takes a long time to finish. |
| 25 | + |
| 26 | +There are two ways how you can use coiled with pytask and pytask-parallel. |
| 27 | + |
| 28 | +1. Run individual tasks in the cloud. |
| 29 | +1. Run your whole workflow in the cloud. |
| 30 | + |
| 31 | +Both approaches are explained below after the setup. |
| 32 | + |
| 33 | +## Setup |
| 34 | + |
| 35 | +Follow coiled's |
| 36 | +[four step short process](https://docs.coiled.io/user_guide/setup/index.html) to set up |
| 37 | +your local environment and configure your cloud provider. |
| 38 | + |
| 39 | +## Running individual tasks |
| 40 | + |
| 41 | +In most projects there are a just couple of tasks that require a lot of resources and |
| 42 | +that you would like to run in a virtual machine in the cloud. |
| 43 | + |
| 44 | +With coiled's |
| 45 | +[serverless functions](https://docs.coiled.io/user_guide/usage/functions/index.html), |
| 46 | +you can define the hardware and software environment for your task. Just decorate the |
| 47 | +task function with a {func}`@coiled.function <coiled.function>` decorator. |
| 48 | + |
| 49 | +```{literalinclude} ../../docs_src/coiled/coiled_functions.py |
| 50 | +``` |
| 51 | + |
| 52 | +To execute the workflow, you need to turn on parallelization by requesting two or more |
| 53 | +workers or specifying one of the parallel backends. Otherwise, the decorated task is run |
| 54 | +locally. |
| 55 | + |
| 56 | +```console |
| 57 | +pytask -n 2 |
| 58 | +pytask --parallel-backend loky |
| 59 | +``` |
| 60 | + |
| 61 | +When you apply the {func}`@task <pytask.task>` decorator to the task, make sure the |
| 62 | +{func}`@coiled.function <coiled.function>` decorator is applied first, or is closer to |
| 63 | +the function. Otherwise, it will be ignored. Add more arguments to the decorator to |
| 64 | +configure the hardware and software environment. |
| 65 | + |
| 66 | +```{literalinclude} ../../docs_src/coiled/coiled_functions_task.py |
| 67 | +``` |
| 68 | + |
| 69 | +```{seealso} |
| 70 | +Serverless functions are more thoroughly explained in |
| 71 | +[coiled's guide](https://docs.coiled.io/user_guide/usage/functions/index.html). |
| 72 | +``` |
| 73 | + |
| 74 | +(coiled-clusters)= |
| 75 | + |
| 76 | +## Running a cluster |
| 77 | + |
| 78 | +It is also possible to launch a cluster and run each task in a worker provided by |
| 79 | +coiled. Usually, it is not necessary and you are better off using coiled's serverless |
| 80 | +functions. |
| 81 | + |
| 82 | +If you want to launch a cluster managed by coiled, register a function that builds an |
| 83 | +executor using {class}`coiled.Cluster`. |
| 84 | + |
| 85 | +```python |
| 86 | +import coiled |
| 87 | +from pytask_parallel import ParallelBackend |
| 88 | +from pytask_parallel import registry |
| 89 | +from concurrent.futures import Executor |
| 90 | + |
| 91 | + |
| 92 | +def _build_coiled_executor(n_workers: int) -> Executor: |
| 93 | + return coiled.Cluster(n_workers=n_workers).get_client().get_executor() |
| 94 | + |
| 95 | + |
| 96 | +registry.register_parallel_backend(ParallelBackend.CUSTOM, _build_coiled_executor) |
| 97 | +``` |
| 98 | + |
| 99 | +Then, execute your workflow with |
| 100 | + |
| 101 | +```console |
| 102 | +pytask --parallel-backend custom |
| 103 | +``` |
0 commit comments