Skip to content

Commit

Permalink
Add Cluster.get_client() method (#6745)
Browse files Browse the repository at this point in the history
  • Loading branch information
jsignell authored Jul 27, 2022
1 parent 942b96d commit 236945a
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
16 changes: 16 additions & 0 deletions distributed/deploy/cluster.py
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,22 @@ def get_logs(self, cluster=True, scheduler=True, workers=True):
def logs(self, *args, **kwargs):
return self.get_logs(*args, **kwargs)

def get_client(self):
"""Return client for the cluster
If a client has already been initialized for the cluster, return that
otherwise initialize a new client object.
"""
from distributed.client import Client

try:
current_client = Client.current()
if current_client and current_client.cluster == self:
return current_client
except ValueError:
pass
return Client(self)

@property
def dashboard_link(self):
try:
Expand Down
11 changes: 11 additions & 0 deletions distributed/deploy/tests/test_local.py
Original file line number Diff line number Diff line change
Expand Up @@ -1267,3 +1267,14 @@ def test_localcluster_start_exception(loop):
loop=loop,
):
pass


def test_localcluster_get_client():
with LocalCluster(
n_workers=0, asynchronous=False, dashboard_address=":0"
) as cluster:
client1 = cluster.get_client()
assert client1.cluster == cluster
client2 = Client(cluster)
assert client1 != client2
assert client2 == cluster.get_client()

0 comments on commit 236945a

Please sign in to comment.