Is there a way to programmatically determine if you are connected to a cluster?

We are in the process of adopting Dask and believe there are cases where running locally is sufficient but in other cases we will definitely need to run on clusters (created via Coiled). Is there a way to programmatically determine if your code is connected to a cluster?

Thanks in advance for your help!

You can call Client.current() to see if a client exists and is connected to a cluster. If it isn’t it will raise a ValueError.

from dask.distributed import Client

def connected_to_cluster():
    try:
        current_client = Client.current()
        return True
    except ValueError:
        return False

If perhaps the question was: there is indeed a distributed client, but you want to know whether it is a LocalCluster or something else, you can look at the client object’s .cluster attribute.

1 Like