-
-
Notifications
You must be signed in to change notification settings - Fork 762
Consistent worker Client instance in get_client
#5467
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
fa4763b
12b8278
ecaf5ab
b7a8d04
d41c82b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -449,7 +449,7 @@ def __init__( | |
| self.has_what = defaultdict(set) | ||
| self.pending_data_per_worker = defaultdict(deque) | ||
| self.nanny = nanny | ||
| self._lock = threading.Lock() | ||
| self._client_lock = threading.Lock() | ||
|
|
||
| self.data_needed = [] | ||
|
|
||
|
|
@@ -3554,11 +3554,7 @@ def validate_state(self): | |
|
|
||
| @property | ||
| def client(self) -> Client: | ||
| with self._lock: | ||
| if self._client: | ||
| return self._client | ||
| else: | ||
| return self._get_client() | ||
| return self._get_client() | ||
|
|
||
| def _get_client(self, timeout=None) -> Client: | ||
| """Get local client attached to this worker | ||
|
|
@@ -3569,56 +3565,64 @@ def _get_client(self, timeout=None) -> Client: | |
| -------- | ||
| get_client | ||
| """ | ||
| if self._client: | ||
| # Assuming `self._client` cannot become None again, this lets us skip the lock | ||
| # once a client already exists. | ||
| return self._client | ||
| with self._client_lock: | ||
| if self._client: | ||
| return self._client | ||
|
gjoseph92 marked this conversation as resolved.
|
||
|
|
||
| if timeout is None: | ||
| timeout = dask.config.get("distributed.comm.timeouts.connect") | ||
|
|
||
| timeout = parse_timedelta(timeout, "s") | ||
| if timeout is None: | ||
| timeout = dask.config.get("distributed.comm.timeouts.connect") | ||
|
|
||
| try: | ||
| from .client import default_client | ||
| timeout = parse_timedelta(timeout, "s") | ||
|
|
||
| client = default_client() | ||
| except ValueError: # no clients found, need to make a new one | ||
| pass | ||
| else: | ||
| # must be lazy import otherwise cyclic import | ||
| from distributed.deploy.cluster import Cluster | ||
| try: | ||
| from .client import default_client | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it would be cleaner to move all imports to the top of the function
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Again, this is just existing code. But I can refactor it. |
||
|
|
||
| if ( | ||
| client.scheduler | ||
| and client.scheduler.address == self.scheduler.address | ||
| # The below conditions should only happen in case a second | ||
| # cluster is alive, e.g. if a submitted task spawned its onwn | ||
| # LocalCluster, see gh4565 | ||
| or ( | ||
| isinstance(client._start_arg, str) | ||
| and client._start_arg == self.scheduler.address | ||
| or isinstance(client._start_arg, Cluster) | ||
| and client._start_arg.scheduler_address == self.scheduler.address | ||
| client = default_client() | ||
| except ValueError: # no clients found, need to make a new one | ||
| pass | ||
| else: | ||
| # must be lazy import otherwise cyclic import | ||
| from distributed.deploy.cluster import Cluster | ||
|
|
||
| if ( | ||
| client.scheduler | ||
| and client.scheduler.address == self.scheduler.address | ||
| # The below conditions should only happen in case a second | ||
| # cluster is alive, e.g. if a submitted task spawned its own | ||
| # LocalCluster, see gh4565 | ||
| or ( | ||
| isinstance(client._start_arg, str) | ||
| and client._start_arg == self.scheduler.address | ||
| or isinstance(client._start_arg, Cluster) | ||
| and client._start_arg.scheduler_address | ||
| == self.scheduler.address | ||
| ) | ||
| ): | ||
| self._client = client | ||
|
|
||
| if not self._client: | ||
| from .client import Client | ||
|
|
||
| asynchronous = self.loop is IOLoop.current() | ||
| self._client = Client( | ||
| self.scheduler, | ||
| loop=self.loop, | ||
| security=self.security, | ||
| set_as_default=True, | ||
| asynchronous=asynchronous, | ||
| direct_to_workers=True, | ||
| name="worker", | ||
| timeout=timeout, | ||
| ) | ||
| ): | ||
| self._client = client | ||
|
|
||
| if not self._client: | ||
| from .client import Client | ||
|
|
||
| asynchronous = self.loop is IOLoop.current() | ||
| self._client = Client( | ||
| self.scheduler, | ||
| loop=self.loop, | ||
| security=self.security, | ||
| set_as_default=True, | ||
| asynchronous=asynchronous, | ||
| direct_to_workers=True, | ||
| name="worker", | ||
| timeout=timeout, | ||
| ) | ||
| Worker._initialized_clients.add(self._client) | ||
| if not asynchronous: | ||
| assert self._client.status == "running" | ||
| Worker._initialized_clients.add(self._client) | ||
| if not asynchronous: | ||
| assert self._client.status == "running" | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why do you need this? Isn't this already done by the sync Client constructor?
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't know. This is all existing code I just indented under the |
||
|
|
||
| return self._client | ||
| return self._client | ||
|
|
||
| def get_current_task(self): | ||
| """Get the key of the task we are currently running | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.