# dask-saturn
Python library for interacting with [Dask](https://dask.org/) clusters in
[Saturn Cloud](https://www.saturncloud.io/).
Dask-Saturn mimics the API of
[Dask-Kubernetes](https://github.com/dask/dask-kubernetes), but allows the user
to interact with clusters created within
[Saturn Cloud](https://www.saturncloud.io/).
## Start cluster
From within a Jupyter notebook, you can start a cluster:
```python
from dask_saturn import SaturnCluster
cluster = SaturnCluster()
cluster
```
By default this will start a dask cluster with the same settings that you have
already set in the Saturn UI or in a prior notebook.
To start the cluster with a certain number of workers using the `n_workers`
option. Similarly, you can set the `scheduler_size`, `worker_size`, and `worker_is_spot`.
> Note: If the cluster is already running then you can't change the settings.
> Attempting to do so will raise a warning.
Use the `shudown_on_close` option to set up a cluster that is tied to the client
kernel. This functions like a regular dask `LocalCluster`, when your jupyter
kernel dies or is restarted, the dask cluster will close.
## Adjust number of workers
Once you have a cluster you can interact with it via the jupyter
widget, or using the `scale` and `adapt` methods.
For example, to manually scale up to 20 workers:
```python
cluster.scale(20)
```
To create an adaptive cluster that controls its own scaling:
```python
cluster.adapt(minimum=1, maximum=20)
```
## Interact with client
To submit tasks to the cluster, you sometimes need access to the
`Client` object. Instantiate this with the cluster as the only argument:
```python
from distributed import Client
client = Client(cluster)
client
```
## Close cluster
To terminate all resources associated with a cluster, use the
`close` method:
```python
cluster.close()
```
## Change settings
To update the settings (such as `n_workers`, `worker_size`, `worker_is_spot`, `nthreads`) on an existing cluster, use the `reset` method:
```python
cluster.reset(n_workers=3)
```
You can also call this without instantiating the cluster first:
```python
cluster = SaturnCluster.reset(n_workers=3)
```
By default, you'll get one worker, but you can change that using the `n_workers` option. Similarly you can override the scheduler and worker hardware settings with `scheduler_size`, `worker_size`. You can display the available size options using `describe_sizes()`:
```python
>>> describe_sizes()
{'medium': 'Medium - 2 cores - 4 GB RAM',
'large': 'Large - 2 cores - 16 GB RAM',
'xlarge': 'XLarge - 4 cores - 32 GB RAM',
'2xlarge': '2XLarge - 8 cores - 64 GB RAM',
'4xlarge': '4XLarge - 16 cores - 128 GB RAM',
'8xlarge': '8XLarge - 32 cores - 256 GB RAM',
'12xlarge': '12XLarge - 48 cores - 384 GB RAM',
'16xlarge': '16XLarge - 64 cores - 512 GB RAM',
'g4dnxlarge': 'T4-XLarge - 4 cores - 16 GB RAM - 1 GPU',
'g4dn4xlarge': 'T4-4XLarge - 16 cores - 64 GB RAM - 1 GPU',
'g4dn8xlarge': 'T4-8XLarge - 32 cores - 128 GB RAM - 1 GPU',
'p32xlarge': 'V100-2XLarge - 8 cores - 61 GB RAM - 1 GPU',
'p38xlarge': 'V100-8XLarge - 32 cores - 244 GB RAM - 4 GPU',
'p316xlarge': 'V100-16XLarge - 64 cores - 488 GB RAM - 8 GPU'}
```
Here's an example:
```python
cluster = SaturnCluster(
scheduler_size="large",
worker_size="2xlarge",
n_workers=3,
)
client = Client(cluster)
client
```
## Connect from outside of Saturn
To connect to your Dask cluster from outside of Saturn, you need to set two environment variables: ``SATURN_TOKEN`` and ``SATURN_BASE_URL``.
To get the values for these you'll need to go Saturn in your browser. Go to where you want to connect a Dask cluster. There will be a button that says:
"Connect Externally". Clicking that will open a modal with the values for ``SATURN_TOKEN`` and ``SATURN_BASE_URL``
Remember - that token is private so don't share it with anyone! It'll be a something like `351e6f2d40bf4d15a0009fc086c602df`
```sh
export SATURN_BASE_URL="https://app.demo.saturnenterprise.io"
export SATURN_TOKEN="351e6f2d40bf4d15a0009fc086c602df"
```
After you have set the environment variables, you can open a Python session and connect to your Dask cluster just as you would inside of Saturn:
```python
from dask_saturn import SaturnCluster
from distributed import Client
cluster = SaturnCluster()
client = Client(cluster)
client
```
When you are done working with the dask cluster make sure to shut it down:
```python
cluster.close()
```
## Sync files to workers
When working with distributed dask clusters, the workers don't have access to the same file system as your client does. So you will see files in your jupyter server that aren't available on the workers. To move files to the workers you can use the `RegisterFiles` plugin and call `sync_files` on any path that you want to update on the workers.
For instance if you have a file structure like:
```
/home/jovyan/project/
|---- utils/
| |---- __init__.py
| |---- hello.py
|
|---- Untitled.ipynb
```
where hello.py contains:
```python
# utils/hello.py
def greet():
return "Hello"
```
If the code in hello.py changes or you add new files to utils, you'll want to push those changes to the workers. After setting up the `SaturnCluster` and the `Client`, register the `RegisterFiles` plugin with the workers. Then every time you make changes to the files in utils, run `sync_files`. The worker plugin makes sure that any new worker that comes up will have any files that you have synced.
```python
from dask_saturn import RegisterFiles, sync_files
client.register_worker_plugin(RegisterFiles())
sync_files(client, "utils")
# If a python script has changed, restart the workers so they will see the changes
client.restart()
# import the function and tell the workers to run it
from util.hello import greet
client.run(greet)
```
> TIP: You can always check the state of the filesystem on your workers by running `client.run(os.listdir)`
## Development
Create/update a dask-saturn conda environment:
```sh
make conda-update
```
Set environment variables to run dask-saturn with a local atlas server:
```sh
export SATURN_BASE_URL=http://dev.localtest.me:8888/
export SATURN_TOKEN=<JUPYTER_SERVER_SATURN_TOKEN>
```
Raw data
{
"_id": null,
"home_page": "https://saturncloud.io/",
"name": "dask-saturn",
"maintainer": "Saturn Cloud Developers",
"docs_url": null,
"requires_python": null,
"maintainer_email": "dev@saturncloud.io",
"keywords": "dask saturn cloud distributed cluster",
"author": null,
"author_email": null,
"download_url": "https://files.pythonhosted.org/packages/68/89/ee6c64993a5a844d9221cfebbbe6e658888e9f63cb7cc06ed7854e8e6afc/dask_saturn-0.4.4.tar.gz",
"platform": null,
"description": "# dask-saturn\nPython library for interacting with [Dask](https://dask.org/) clusters in\n[Saturn Cloud](https://www.saturncloud.io/).\n\nDask-Saturn mimics the API of\n[Dask-Kubernetes](https://github.com/dask/dask-kubernetes), but allows the user\nto interact with clusters created within\n[Saturn Cloud](https://www.saturncloud.io/).\n\n## Start cluster\nFrom within a Jupyter notebook, you can start a cluster:\n\n```python\nfrom dask_saturn import SaturnCluster\n\ncluster = SaturnCluster()\ncluster\n```\n\nBy default this will start a dask cluster with the same settings that you have\nalready set in the Saturn UI or in a prior notebook.\n\nTo start the cluster with a certain number of workers using the `n_workers`\noption. Similarly, you can set the `scheduler_size`, `worker_size`, and `worker_is_spot`.\n\n> Note: If the cluster is already running then you can't change the settings.\n> Attempting to do so will raise a warning.\n\nUse the `shudown_on_close` option to set up a cluster that is tied to the client\nkernel. This functions like a regular dask `LocalCluster`, when your jupyter\nkernel dies or is restarted, the dask cluster will close.\n\n## Adjust number of workers\nOnce you have a cluster you can interact with it via the jupyter\nwidget, or using the `scale` and `adapt` methods.\n\nFor example, to manually scale up to 20 workers:\n\n```python\ncluster.scale(20)\n```\n\nTo create an adaptive cluster that controls its own scaling:\n\n```python\ncluster.adapt(minimum=1, maximum=20)\n```\n\n## Interact with client\nTo submit tasks to the cluster, you sometimes need access to the\n`Client` object. Instantiate this with the cluster as the only argument:\n\n```python\nfrom distributed import Client\n\nclient = Client(cluster)\nclient\n```\n\n## Close cluster\n\nTo terminate all resources associated with a cluster, use the\n`close` method:\n\n```python\ncluster.close()\n```\n\n## Change settings\n\nTo update the settings (such as `n_workers`, `worker_size`, `worker_is_spot`, `nthreads`) on an existing cluster, use the `reset` method:\n\n```python\ncluster.reset(n_workers=3)\n```\n\nYou can also call this without instantiating the cluster first:\n\n```python\ncluster = SaturnCluster.reset(n_workers=3)\n```\n\nBy default, you'll get one worker, but you can change that using the `n_workers` option. Similarly you can override the scheduler and worker hardware settings with `scheduler_size`, `worker_size`. You can display the available size options using `describe_sizes()`:\n\n```python\n>>> describe_sizes()\n{'medium': 'Medium - 2 cores - 4 GB RAM',\n 'large': 'Large - 2 cores - 16 GB RAM',\n 'xlarge': 'XLarge - 4 cores - 32 GB RAM',\n '2xlarge': '2XLarge - 8 cores - 64 GB RAM',\n '4xlarge': '4XLarge - 16 cores - 128 GB RAM',\n '8xlarge': '8XLarge - 32 cores - 256 GB RAM',\n '12xlarge': '12XLarge - 48 cores - 384 GB RAM',\n '16xlarge': '16XLarge - 64 cores - 512 GB RAM',\n 'g4dnxlarge': 'T4-XLarge - 4 cores - 16 GB RAM - 1 GPU',\n 'g4dn4xlarge': 'T4-4XLarge - 16 cores - 64 GB RAM - 1 GPU',\n 'g4dn8xlarge': 'T4-8XLarge - 32 cores - 128 GB RAM - 1 GPU',\n 'p32xlarge': 'V100-2XLarge - 8 cores - 61 GB RAM - 1 GPU',\n 'p38xlarge': 'V100-8XLarge - 32 cores - 244 GB RAM - 4 GPU',\n 'p316xlarge': 'V100-16XLarge - 64 cores - 488 GB RAM - 8 GPU'}\n```\n\nHere's an example:\n\n```python\ncluster = SaturnCluster(\n scheduler_size=\"large\",\n worker_size=\"2xlarge\",\n n_workers=3,\n)\nclient = Client(cluster)\nclient\n```\n\n## Connect from outside of Saturn\n\nTo connect to your Dask cluster from outside of Saturn, you need to set two environment variables: ``SATURN_TOKEN`` and ``SATURN_BASE_URL``.\n\nTo get the values for these you'll need to go Saturn in your browser. Go to where you want to connect a Dask cluster. There will be a button that says:\n\"Connect Externally\". Clicking that will open a modal with the values for ``SATURN_TOKEN`` and ``SATURN_BASE_URL``\n\nRemember - that token is private so don't share it with anyone! It'll be a something like `351e6f2d40bf4d15a0009fc086c602df`\n\n```sh\nexport SATURN_BASE_URL=\"https://app.demo.saturnenterprise.io\"\nexport SATURN_TOKEN=\"351e6f2d40bf4d15a0009fc086c602df\"\n```\n\nAfter you have set the environment variables, you can open a Python session and connect to your Dask cluster just as you would inside of Saturn:\n\n```python\nfrom dask_saturn import SaturnCluster\nfrom distributed import Client\n\ncluster = SaturnCluster()\nclient = Client(cluster)\nclient\n```\n\nWhen you are done working with the dask cluster make sure to shut it down:\n\n```python\ncluster.close()\n```\n\n## Sync files to workers\n\nWhen working with distributed dask clusters, the workers don't have access to the same file system as your client does. So you will see files in your jupyter server that aren't available on the workers. To move files to the workers you can use the `RegisterFiles` plugin and call `sync_files` on any path that you want to update on the workers.\n\nFor instance if you have a file structure like:\n```\n/home/jovyan/project/\n|---- utils/\n| |---- __init__.py\n| |---- hello.py\n|\n|---- Untitled.ipynb\n```\n\nwhere hello.py contains:\n\n```python\n# utils/hello.py\ndef greet():\n return \"Hello\"\n```\n\nIf the code in hello.py changes or you add new files to utils, you'll want to push those changes to the workers. After setting up the `SaturnCluster` and the `Client`, register the `RegisterFiles` plugin with the workers. Then every time you make changes to the files in utils, run `sync_files`. The worker plugin makes sure that any new worker that comes up will have any files that you have synced.\n\n```python\nfrom dask_saturn import RegisterFiles, sync_files\n\nclient.register_worker_plugin(RegisterFiles())\nsync_files(client, \"utils\")\n\n# If a python script has changed, restart the workers so they will see the changes\nclient.restart()\n\n# import the function and tell the workers to run it\nfrom util.hello import greet\nclient.run(greet)\n```\n\n> TIP: You can always check the state of the filesystem on your workers by running `client.run(os.listdir)`\n\n## Development\n\nCreate/update a dask-saturn conda environment:\n\n```sh\nmake conda-update\n```\n\nSet environment variables to run dask-saturn with a local atlas server:\n\n```sh\nexport SATURN_BASE_URL=http://dev.localtest.me:8888/\nexport SATURN_TOKEN=<JUPYTER_SERVER_SATURN_TOKEN>\n```\n",
"bugtrack_url": null,
"license": "BSD-3-Clause",
"summary": "Dask Cluster objects in Saturn Cloud",
"version": "0.4.4",
"project_urls": {
"Documentation": "http://docs.saturncloud.io",
"Homepage": "https://saturncloud.io/",
"Issue Tracker": "https://github.com/saturncloud/dask-saturn/issues",
"Source": "https://github.com/saturncloud/dask-saturn"
},
"split_keywords": [
"dask",
"saturn",
"cloud",
"distributed",
"cluster"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "2b42d491a522b6e8c4297c9c032b01baba442542a129948417d5cb5f9bf5ed4c",
"md5": "d6b2e670c37b9dab10e64faf9d9a0b67",
"sha256": "153f8b7a7c40e2fa0669a82cf030c6e1b5e93708b6259d6948740a6ce95ddd93"
},
"downloads": -1,
"filename": "dask_saturn-0.4.4-py3-none-any.whl",
"has_sig": false,
"md5_digest": "d6b2e670c37b9dab10e64faf9d9a0b67",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": null,
"size": 16306,
"upload_time": "2024-10-17T20:31:18",
"upload_time_iso_8601": "2024-10-17T20:31:18.312674Z",
"url": "https://files.pythonhosted.org/packages/2b/42/d491a522b6e8c4297c9c032b01baba442542a129948417d5cb5f9bf5ed4c/dask_saturn-0.4.4-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "6889ee6c64993a5a844d9221cfebbbe6e658888e9f63cb7cc06ed7854e8e6afc",
"md5": "dd86dad032db3f8a1843cbe8a421f7e7",
"sha256": "8b697b0574125b16fa311e514740021f7ef7bd6e8050af5faf3323651db9c956"
},
"downloads": -1,
"filename": "dask_saturn-0.4.4.tar.gz",
"has_sig": false,
"md5_digest": "dd86dad032db3f8a1843cbe8a421f7e7",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 35723,
"upload_time": "2024-10-17T20:31:19",
"upload_time_iso_8601": "2024-10-17T20:31:19.846787Z",
"url": "https://files.pythonhosted.org/packages/68/89/ee6c64993a5a844d9221cfebbbe6e658888e9f63cb7cc06ed7854e8e6afc/dask_saturn-0.4.4.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-10-17 20:31:19",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "saturncloud",
"github_project": "dask-saturn",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"lcname": "dask-saturn"
}