# ResourceManager Tasks
# ResourceManager
class
prefect.tasks.core.resource_manager.ResourceManager
(resource_class, name=None, init_task_kwargs=None, setup_task_kwargs=None, cleanup_task_kwargs=None)[source]An object for managing temporary resources.
Used as a context manager, ResourceManager
objects create tasks to setup and cleanup temporary objects used within a block of tasks. Examples might include temporary Dask/Spark clusters, Docker containers, etc...
ResourceManager
objects are usually created using the resource_manager
decorator, but can be created directly using this class if desired.
For more information, see the docs for resource_manager
.
Args:
resource_class (Callable)
: A callable (usually the class itself) for creating an object that follows theResourceManager
protocol.name (str, optional)
: The resource name - defaults to the name of the decorated class.init_task_kwargs (dict, optional)
: keyword arguments that will be passed to theTask
constructor for theinit
task.setup_task_kwargs (dict, optional)
: keyword arguments that will be passed to theTask
constructor for thesetup
task.cleanup_task_kwargs (dict, optional)
: keyword arguments that will be passed to theTask
constructor for thecleanup
task.
Here's an example resource manager for creating a temporary local dask cluster as part of the flow.
from prefect import resource_manager
from dask.distributed import Client
@resource_manager
class DaskCluster:
def __init__(self, n_workers):
self.n_workers = n_workers
def setup(self):
"Create a local dask cluster"
return Client(n_workers=self.n_workers)
def cleanup(self, client):
"Cleanup the local dask cluster"
client.close()
To use the DaskCluster
resource manager as part of your Flow, you can use DaskCluster
as a context manager:
with Flow("example") as flow:
n_workers = Parameter("n_workers")
with DaskCluster(n_workers=n_workers) as client:
some_task(client)
some_other_task(client)
# Functions
top-level functions: |
---|
prefect.tasks.core.resource_manager.resource_manager (resource_class=None, name=None, init_task_kwargs=None, setup_task_kwargs=None, cleanup_task_kwargs=None)[source] |
A decorator for creating a
Here's an example resource manager for creating a temporary local dask cluster as part of the flow.
To use the DaskCluster resource manager as part of your Flow, you can use DaskCluster as a context manager:
The Task returned by entering the DaskCluster context (i.e. the client part of as client ) is the output of the setup method on the ResourceManager class. A Task is automatically added to call the cleanup method (closing the Dask cluster) after all tasks under the context have completed. By default this cleanup task is configured with a trigger to always run if the setup task succeeds, and won't be set as a reference task. |
This documentation was auto-generated from commit bd9182e
on July 31, 2024 at 18:02 UTC
← Redis Tasks RSS Tasks →