|
14 | 14 | # See the License for the specific language governing permissions and
|
15 | 15 | # limitations under the License.
|
16 | 16 | #
|
17 |
| -import asyncio |
18 | 17 | import json
|
19 | 18 | import pathlib
|
20 | 19 | import re
|
@@ -227,16 +226,39 @@ def __init__(
|
227 | 226 | # Lazy load the Endpoint gca_resource until needed
|
228 | 227 | self._gca_resource = gca_endpoint_compat.Endpoint(name=endpoint_name)
|
229 | 228 |
|
230 |
| - ( |
231 |
| - self._prediction_client, |
232 |
| - self._prediction_async_client, |
233 |
| - ) = self._instantiate_prediction_clients( |
234 |
| - location=self.location, |
235 |
| - credentials=credentials, |
236 |
| - ) |
237 | 229 | self.authorized_session = None
|
238 | 230 | self.raw_predict_request_url = None
|
239 | 231 |
|
| 232 | + @property |
| 233 | + def _prediction_client(self) -> utils.PredictionClientWithOverride: |
| 234 | + # The attribute might not exist due to issues in |
| 235 | + # `VertexAiResourceNounWithFutureManager._sync_object_with_future_result` |
| 236 | + # We should switch to @functools.cached_property once its available. |
| 237 | + if not getattr(self, "_prediction_client_value", None): |
| 238 | + self._prediction_client_value = initializer.global_config.create_client( |
| 239 | + client_class=utils.PredictionClientWithOverride, |
| 240 | + credentials=self.credentials, |
| 241 | + location_override=self.location, |
| 242 | + prediction_client=True, |
| 243 | + ) |
| 244 | + return self._prediction_client_value |
| 245 | + |
| 246 | + @property |
| 247 | + def _prediction_async_client(self) -> utils.PredictionAsyncClientWithOverride: |
| 248 | + # The attribute might not exist due to issues in |
| 249 | + # `VertexAiResourceNounWithFutureManager._sync_object_with_future_result` |
| 250 | + # We should switch to @functools.cached_property once its available. |
| 251 | + if not getattr(self, "_prediction_async_client_value", None): |
| 252 | + self._prediction_async_client_value = ( |
| 253 | + initializer.global_config.create_client( |
| 254 | + client_class=utils.PredictionAsyncClientWithOverride, |
| 255 | + credentials=self.credentials, |
| 256 | + location_override=self.location, |
| 257 | + prediction_client=True, |
| 258 | + ) |
| 259 | + ) |
| 260 | + return self._prediction_async_client_value |
| 261 | + |
240 | 262 | def _skipped_getter_call(self) -> bool:
|
241 | 263 | """Check if GAPIC resource was populated by call to get/list API methods
|
242 | 264 |
|
@@ -575,14 +597,6 @@ def _construct_sdk_resource_from_gapic(
|
575 | 597 | location=location,
|
576 | 598 | credentials=credentials,
|
577 | 599 | )
|
578 |
| - |
579 |
| - ( |
580 |
| - endpoint._prediction_client, |
581 |
| - endpoint._prediction_async_client, |
582 |
| - ) = cls._instantiate_prediction_clients( |
583 |
| - location=endpoint.location, |
584 |
| - credentials=credentials, |
585 |
| - ) |
586 | 600 | endpoint.authorized_session = None
|
587 | 601 | endpoint.raw_predict_request_url = None
|
588 | 602 |
|
@@ -1390,53 +1404,6 @@ def _undeploy(
|
1390 | 1404 | # update local resource
|
1391 | 1405 | self._sync_gca_resource()
|
1392 | 1406 |
|
1393 |
| - @staticmethod |
1394 |
| - def _instantiate_prediction_clients( |
1395 |
| - location: Optional[str] = None, |
1396 |
| - credentials: Optional[auth_credentials.Credentials] = None, |
1397 |
| - ) -> Tuple[ |
1398 |
| - utils.PredictionClientWithOverride, utils.PredictionAsyncClientWithOverride |
1399 |
| - ]: |
1400 |
| - """Helper method to instantiates prediction client with optional |
1401 |
| - overrides for this endpoint. |
1402 |
| -
|
1403 |
| - Args: |
1404 |
| - location (str): The location of this endpoint. |
1405 |
| - credentials (google.auth.credentials.Credentials): |
1406 |
| - Optional custom credentials to use when accessing interacting with |
1407 |
| - the prediction client. |
1408 |
| -
|
1409 |
| - Returns: |
1410 |
| - prediction_client (prediction_service_client.PredictionServiceClient): |
1411 |
| - prediction_async_client (PredictionServiceAsyncClient): |
1412 |
| - Initialized prediction clients with optional overrides. |
1413 |
| - """ |
1414 |
| - |
1415 |
| - # Creating an event loop if needed. |
1416 |
| - # PredictionServiceAsyncClient constructor calls `asyncio.get_event_loop`, |
1417 |
| - # which fails when there is no event loop (which does not exist by default |
1418 |
| - # in non-main threads in thread pool used when `sync=False`). |
1419 |
| - try: |
1420 |
| - asyncio.get_event_loop() |
1421 |
| - except RuntimeError: |
1422 |
| - asyncio.set_event_loop(asyncio.new_event_loop()) |
1423 |
| - |
1424 |
| - async_client = initializer.global_config.create_client( |
1425 |
| - client_class=utils.PredictionAsyncClientWithOverride, |
1426 |
| - credentials=credentials, |
1427 |
| - location_override=location, |
1428 |
| - prediction_client=True, |
1429 |
| - ) |
1430 |
| - # We could use `client = async_client._client`, but then client would be |
1431 |
| - # a concrete `PredictionServiceClient`, not `PredictionClientWithOverride`. |
1432 |
| - client = initializer.global_config.create_client( |
1433 |
| - client_class=utils.PredictionClientWithOverride, |
1434 |
| - credentials=credentials, |
1435 |
| - location_override=location, |
1436 |
| - prediction_client=True, |
1437 |
| - ) |
1438 |
| - return (client, async_client) |
1439 |
| - |
1440 | 1407 | def update(
|
1441 | 1408 | self,
|
1442 | 1409 | display_name: Optional[str] = None,
|
|
0 commit comments