Skip to content

Commit

Permalink
[Placement Group] Export bundle reservation check method only once. (#…
Browse files Browse the repository at this point in the history
…11153)

* Export bundle reservation check method only once.

* Addressed code review.
  • Loading branch information
rkooo567 committed Oct 8, 2020
1 parent 74e9647 commit 6cb0020
Showing 1 changed file with 20 additions and 3 deletions.
23 changes: 20 additions & 3 deletions python/ray/util/placement_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,25 @@
import ray
from ray._raylet import PlacementGroupID, ObjectRef

bundle_reservation_check = None


# We need to import this method to use for ready API.
# But ray.remote is only available in runtime, and
# if we define this method inside ready method, this function is
# exported whenever ready is called, which can impact performance,
# https://github.com/ray-project/ray/issues/6240.
def _export_bundle_reservation_check_method_if_needed():
global bundle_reservation_check
if bundle_reservation_check:
return

@ray.remote(num_cpus=0, max_calls=0)
def bundle_reservation_check_func(placement_group):
return placement_group

bundle_reservation_check = bundle_reservation_check_func


class PlacementGroup:
"""A handle to a placement group."""
Expand Down Expand Up @@ -33,9 +52,7 @@ def ready(self) -> ObjectRef:
"""
self._fill_bundle_cache_if_needed()

@ray.remote(num_cpus=0, max_calls=0)
def bundle_reservation_check(placement_group):
return placement_group
_export_bundle_reservation_check_method_if_needed()

assert len(self.bundle_cache) != 0, (
"ready() cannot be called on placement group object with a "
Expand Down

0 comments on commit 6cb0020

Please sign in to comment.