def maybe_create_placement_group(name='', max_retries=10):
  """Creates placement_group group or reuses existing one. Crash if unable to create
    placement_group group. If name is empty, ignores request."""

  if not name:
    return

  client = get_ec2_client()
  while True:
    try:
      client.describe_placement_groups(GroupNames=[name])
      print("Reusing placement_group group: " + name)
      break  # no Exception means group name was found
    except Exception:
      print("Creating placement_group group: " + name)
      try:
        _response = client.create_placement_group(GroupName=name,
                                                  Strategy='cluster')
      except Exception:
        # because of race can get InvalidPlacementGroup.Duplicate
        pass

  counter = 0
  while True:
    try:
      res = client.describe_placement_groups(GroupNames=[name])
      res_entry = res['PlacementGroups'][0]
      if res_entry['State'] == 'available':
        assert res_entry['Strategy'] == 'cluster'
        break
    except Exception as e:
      print("Got exception: %s" % (e,))
    counter += 1
    if counter >= max_retries:
      assert False, f'Failed to create placement_group group {name} in {max_retries} attempts'
    time.sleep(RETRY_INTERVAL_SEC)
def maybe_create_placement_group ( name = '' , max_retries = 10 ) : if not name : return client = get_ec2_client ( ) while True : try : client . describe_placement_groups ( GroupNames = [ name ] ) print ( "Reusing placement_group group: " + name ) break except Exception : print ( "Creating placement_group group: " + name ) try : _response = client . create_placement_group ( GroupName = name , Strategy = 'cluster' ) except Exception : pass counter = 0 while True : try : res = client . describe_placement_groups ( GroupNames = [ name ] ) res_entry = res [ 'PlacementGroups' ] [ 0 ] if res_entry [ 'State' ] == 'available' : assert res_entry [ 'Strategy' ] == 'cluster' break except Exception as e : print ( "Got exception: %s" % ( e , ) ) counter += 1 if counter >= max_retries : assert False , f'Failed to create placement_group group {name} in {max_retries} attempts' time . sleep ( RETRY_INTERVAL_SEC )
Creates placement_group group or reuses existing one. Crash if unable to create
    placement_group group. If name is empty, ignores request.