| 280 | |
| 281 | |
| 282 | class AsyncDstackClient(BaseClient): |
| 283 | PATH_PREFIX = "/" |
| 284 | |
| 285 | def __init__( |
| 286 | self, |
| 287 | endpoint: str | None = None, |
| 288 | *, |
| 289 | use_sync_http: bool = False, |
| 290 | timeout: float = 3, |
| 291 | ): |
| 292 | """Initialize async client with HTTP or Unix-socket transport. |
| 293 | |
| 294 | Args: |
| 295 | endpoint: HTTP/HTTPS URL or Unix socket path |
| 296 | use_sync_http: If True, use sync HTTP client internally |
| 297 | timeout: Timeout in seconds |
| 298 | |
| 299 | """ |
| 300 | endpoint = get_endpoint(endpoint) |
| 301 | self.use_sync_http = use_sync_http |
| 302 | self._client: Optional[httpx.AsyncClient] = None |
| 303 | self._sync_client: Optional[httpx.Client] = None |
| 304 | self._client_ref_count = 0 |
| 305 | self._timeout = timeout |
| 306 | |
| 307 | if endpoint.startswith("http://") or endpoint.startswith("https://"): |
| 308 | self.async_transport = httpx.AsyncHTTPTransport() |
| 309 | self.sync_transport = httpx.HTTPTransport() |
| 310 | self.base_url = endpoint |
| 311 | else: |
| 312 | # Check if Unix socket file exists |
| 313 | if endpoint.startswith("/") and not os.path.exists(endpoint): |
| 314 | raise FileNotFoundError(f"Unix socket file {endpoint} does not exist") |
| 315 | self.async_transport = httpx.AsyncHTTPTransport(uds=endpoint) |
| 316 | self.sync_transport = httpx.HTTPTransport(uds=endpoint) |
| 317 | self.base_url = "http://localhost" |
| 318 | |
| 319 | def _get_client(self) -> httpx.AsyncClient: |
| 320 | if self._client is None: |
| 321 | self._client = httpx.AsyncClient( |
| 322 | transport=self.async_transport, |
| 323 | base_url=self.base_url, |
| 324 | timeout=self._timeout, |
| 325 | ) |
| 326 | return self._client |
| 327 | |
| 328 | def _get_sync_client(self) -> httpx.Client: |
| 329 | if self._sync_client is None: |
| 330 | self._sync_client = httpx.Client( |
| 331 | transport=self.sync_transport, |
| 332 | base_url=self.base_url, |
| 333 | timeout=self._timeout, |
| 334 | ) |
| 335 | return self._sync_client |
| 336 | |
| 337 | async def _send_rpc_request( |
| 338 | self, method: str, payload: Dict[str, Any] |
| 339 | ) -> Dict[str, Any]: |
no outgoing calls