from__future__importannotationsimportasyncioimporttypingastimportattrsfromsimple_diimportProvidefromsimple_diimportinjectfrombentoml._internal.configuration.containersimportBentoMLContainerfrombentoml.exceptionsimportBentoMLExceptionfrom.factoryimportServiceift.TYPE_CHECKING:from_bentoml_impl.client.proxyimportRemoteProxyT=t.TypeVar("T")_dependencies:list[Dependency[t.Any]]=[]asyncdefcleanup()->None:tasks=[dep.close()fordepin_dependencies]awaitasyncio.gather(*tasks)_dependencies.clear()@attrs.defineclassDependency(t.Generic[T]):on:Service[T]|None=Nonedeployment:str|None=Nonecluster:str|None=Noneurl:str|None=None_resolved:t.Any=attrs.field(default=None,init=False)@t.overloaddefget(self:Dependency[None])->RemoteProxy[t.Any]:...@t.overloaddefget(self:Dependency[T])->T:...@injectdefget(self:Dependency[T],*,runner_mapping:dict[str,str]=Provide[BentoMLContainer.remote_runner_mapping],)->T|RemoteProxy[t.Any]:from_bentoml_impl.client.proxyimportRemoteProxymedia_type="application/json"ifself.deploymentandself.url:raiseBentoMLException("Cannot specify both deployment and url")ifself.deployment:client=BentoMLContainer.rest_api_client.get()deployment=client.v2.get_deployment(self.deployment,self.cluster)try:self.url=deployment.urls[0]exceptIndexError:raiseBentoMLException(f"Deployment {self.deployment} does not have any URLs")elifnotself.url:ifself.onisNone:raiseBentoMLException("Must specify one of on, deployment or url")if(key:=self.on.name)inrunner_mapping:self.url=runner_mapping[key]media_type="application/vnd.bentoml+pickle"else:returnself.on()returnRemoteProxy(self.url,service=self.on,media_type=media_type).as_service()@t.overloaddef__get__(self,instance:None,owner:t.Any)->t.Self:...@t.overloaddef__get__(self:Dependency[None],instance:t.Any,owner:t.Any)->RemoteProxy[t.Any]:...@t.overloaddef__get__(self:Dependency[T],instance:t.Any,owner:t.Any)->T:...def__get__(self:Dependency[T],instance:t.Any,owner:t.Any)->Dependency[T]|RemoteProxy[t.Any]|T:ifinstanceisNone:returnselfifself._resolvedisNone:self._resolved=self.get()_dependencies.append(self)returnself._resolveddef__getattr__(self,name:str)->t.Any:raiseAttributeError("Dependency must be accessed as a class attribute")asyncdefclose(self)->None:ifself._resolvedisNone:returnremote_proxy=t.cast("RemoteProxy[t.Any]",self._resolved)ifasyncio.iscoroutinefunction(getattr(remote_proxy,"close",None)):awaitremote_proxy.close()@t.overloaddefdepends(*,url:str|None=...,deployment:str|None=...,cluster:str|None=...,)->Dependency[None]:...@t.overloaddefdepends(on:Service[T],*,url:str|None=...,deployment:str|None=...,cluster:str|None=...,)->Dependency[T]:...
[docs]defdepends(on:Service[T]|None=None,*,url:str|None=None,deployment:str|None=None,cluster:str|None=None,)->Dependency[T]:"""Create a dependency on other service or deployment Args: on: Service[T] | None: The service to depend on. url: str | None: The URL of the service to depend on. deployment: str | None: The deployment of the service to depend on. cluster: str | None: The cluster of the service to depend on. Examples: .. code-block:: python @bentoml.service class MyService: # depends on a service svc_a = bentoml.depends(SVC_A) # depends on a deployment svc_b = bentoml.depends(deployment="ci-iris") # depends on a remote service with url svc_c = bentoml.depends(url="http://192.168.1.1:3000") # For the latter two cases, the service can be given to provide more accurate types: svc_d = bentoml.depends(url="http://192.168.1.1:3000", on=SVC_D) """ifonisnotNoneandnotisinstance(on,Service):raiseTypeError("depends() expects a class decorated with @bentoml.service()")returnDependency(on,url=url,deployment=deployment,cluster=cluster)