
    %`iM                        d dl Z d dlZd dlmZmZmZmZ d dlZd dl	m
Z
 d dlmZ d dlmZ d dlmZ d dlmZ d dlmZmZ  e j        e          Ze G d	 d
e                      Zda ej                    Zee
defd                        ZdS )    N)AnyDictListOptional)client_mode_hook)actors)(parse_pg_formatted_resources_to_original)TaskID)
RuntimeEnv)
Deprecated	PublicAPIc                   t   e Zd ZdZd Z edd          deeef         fd            Z	e
 edd          d	                         Zdefd
Ze
 edd          d                         ZdefdZdefdZe
 edd          d                         Zdee         fdZdefdZdee         fdZdee         fdZe
 edd          d                         Zdee         fdZdee         fdZe
d             Ze
d             Ze
 edd          d                         Zdee         fdZe
d             Zd Zd  Z e
d!             Z!e
d"             Z"e
d#             Z# ed$d          deee$e         f         fd%            Z%deee$e         f         fd&Z&deee$e         f         fd'Z'd(S ))RuntimeContextz)A class used for getting runtime context.c                     |J || _         d S N)workerselfr   s     g/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/ray/runtime_context.py__init__zRuntimeContext.__init__   s    !!!    z4Use get_xxx_id() methods to get relevant ids insteadT)messagewarningreturnc                     | j         | j        | j        d}| j        j        t
          j        j        j        k    r"| j        
| j        |d<   | j	        
| j	        |d<   |S )zxGet a dictionary of the current context.

        Returns:
            dict: Dictionary of the current context.
        )job_idnode_id	namespaceNtask_idactor_id)
r   r   r   r   moderay_privateWORKER_MODEr   r    )r   contexts     r   getzRuntimeContext.get   sf     k|
 

 ;s|2>>>|'%)\	"}(&*m
#r   zUse get_job_id() insteadc                 J    | j         j        }|                                rJ |S )a  Get current job ID for this worker or driver.

        Job ID is the id of your Ray drivers that create tasks or actors.

        Returns:
            If called by a driver, this returns the job ID. If called in
            a task, return the job ID of the associated driver.

        )r   current_job_idis_nilr   r   s     r   r   zRuntimeContext.job_id.   s'     +==??"""r   c                 |    t          j                    s
J d            | j        j        }|                                S )a  Get current job ID for this worker or driver.

        Job ID is the id of your Ray drivers that create tasks or actors.

        Returns:
            If called by a driver, this returns the job ID. If called in
            a task, return the job ID of the associated driver. The
            job ID will be hex format.

        Raises:
            AssertionError: If not called in a driver or worker. Generally,
                this means that ray.init() was not called.
        z=Job ID is not available because Ray has not been initialized.)r"   is_initializedr   r(   hexr*   s     r   
get_job_idzRuntimeContext.get_job_id>   sF       	K 	KJ	K 	K +zz||r   zUse get_node_id() insteadc                 J    | j         j        }|                                rJ |S )aR  Get the ID for the node that this process is running on.

        This can be called from within a driver, task, or actor.
        When called from a driver that is connected to a remote Ray cluster using
        Ray Client, this returns the ID of the head node.

        Returns:
            A node id for this worker or driver.
        )r   current_node_idr)   r   r   s     r   r   zRuntimeContext.node_idR   s)     +->>#####r   c                 |    t          j                    s
J d            | j        j        }|                                S )a  Get the ID for the node that this process is running on.

        This can be called from within a driver, task, or actor.
        When called from a driver that is connected to a remote Ray cluster using
        Ray Client, this returns the ID of the head node.

        Returns:
            A node id in hex format for this worker or driver.

        Raises:
            AssertionError: If not called in a driver or worker. Generally,
                this means that ray.init() was not called.
        z>Node ID is not available because Ray has not been initialized.)r"   r,   r   r0   r-   r1   s     r   get_node_idzRuntimeContext.get_node_idb   sF       	L 	LK	L 	L +-{{}}r   c                 x    t          j                    s
J d            | j        j                                        S )zGet current worker ID for this worker or driver process.

        Returns:
            A worker id in hex format for this worker or driver process.
        z@Worker ID is not available because Ray has not been initialized.)r"   r,   r   	worker_idr-   r   s    r   get_worker_idzRuntimeContext.get_worker_idv   sE       	N 	NM	N 	N {$((***r   zUse get_task_id() insteadc                     | j         j        t          j        j         j        k    sJ d| j         j                     |                                 }|                                s|ndS )aZ  Get current task ID for this worker.

        Task ID is the id of a Ray task.
        This shouldn't be used in a driver process.

        Example:

            .. testcode::

                import ray

                @ray.remote
                class Actor:
                    def ready(self):
                        return True

                @ray.remote
                def f():
                    return True

                # All the below code generates different task ids.
                # Task ids are available for actor creation.
                a = Actor.remote()
                # Task ids are available for actor tasks.
                a.ready.remote()
                # Task ids are available for normal tasks.
                f.remote()

        Returns:
            The current worker's task id. None if there's no task id.
        ZThis method is only available when the process is a                 worker. Current mode: N)r   r!   r"   r#   r$   _get_current_task_idr)   r   r   s     r   r   zRuntimeContext.task_id   so    H K 3 ????;(,(8; ; @?? ++--%nn..8wwD8r   c                    | j         j        t          j        j         j        k    r)t
                              d| j         j                    dS |                                 }|                                s|	                                ndS )a  Get current task ID for this worker.

        Task ID is the id of a Ray task. The ID will be in hex format.
        This shouldn't be used in a driver process.

        Example:

            .. testcode::

                import ray

                @ray.remote
                class Actor:
                    def get_task_id(self):
                        return ray.get_runtime_context().get_task_id()

                @ray.remote
                def get_task_id():
                    return ray.get_runtime_context().get_task_id()

                # All the below code generates different task ids.
                a = Actor.remote()
                # Task ids are available for actor tasks.
                print(ray.get(a.get_task_id.remote()))
                # Task ids are available for normal tasks.
                print(ray.get(get_task_id.remote()))

            .. testoutput::
                :options: +MOCK

                16310a0f0a45af5c2746a0e6efb235c0962896a201000000
                c2668a65bda616c1ffffffffffffffffffffffff01000000

        Returns:
            The current worker's task id in hex. None if there's no task id.
        JThis method is only available when the process is a worker. Current mode: N)
r   r!   r"   r#   r$   loggerr   r:   r)   r-   r;   s     r   get_task_idzRuntimeContext.get_task_id   s    L ;s|2>>>NN<)-)9< <   4++--$+NN$4$4>w{{}}}$>r   c                     | j         j        S r   )r   current_task_idr6   s    r   r:   z#RuntimeContext._get_current_task_id   s    {**r   c                     | j         j        t          j        j         j        k    r)t
                              d| j         j                    dS | j         j        S )aB  Get current task name for this worker.

        Task name by default is the task's funciton call string. It can also be
        specified in options when triggering a task.

        Example:

            .. testcode::

                import ray

                @ray.remote
                class Actor:
                    def get_task_name(self):
                        return ray.get_runtime_context().get_task_name()

                @ray.remote
                class AsyncActor:
                    async def get_task_name(self):
                        return ray.get_runtime_context().get_task_name()

                @ray.remote
                def get_task_name():
                    return ray.get_runtime_context().get_task_name()

                a = Actor.remote()
                b = AsyncActor.remote()
                # Task names are available for actor tasks.
                print(ray.get(a.get_task_name.remote()))
                # Task names are avaiable for async actor tasks.
                print(ray.get(b.get_task_name.remote()))
                # Task names are available for normal tasks.
                # Get default task name
                print(ray.get(get_task_name.remote()))
                # Get specified task name
                print(ray.get(get_task_name.options(name="task_name").remote()))

            .. testoutput::
                :options: +MOCK

                Actor.get_task_name
                AsyncActor.get_task_name
                get_task_name
                task_nams

        Returns:
            The current worker's task name
        r=   N)r   r!   r"   r#   r$   r>   r   current_task_namer6   s    r   get_task_namezRuntimeContext.get_task_name   s\    d ;s|2>>>NN<)-)9< <   4{,,r   c                     | j         j        t          j        j         j        k    r)t
                              d| j         j                    dS | j         j        S )a  Get current task function name string for this worker.

        Example:

            .. testcode::

                import ray

                @ray.remote
                class Actor:
                    def get_task_function_name(self):
                        return ray.get_runtime_context().get_task_function_name()

                @ray.remote
                class AsyncActor:
                    async def get_task_function_name(self):
                        return ray.get_runtime_context().get_task_function_name()

                @ray.remote
                def get_task_function_name():
                    return ray.get_runtime_context().get_task_function_name()

                a = Actor.remote()
                b = AsyncActor.remote()
                # Task functions are available for actor tasks.
                print(ray.get(a.get_task_function_name.remote()))
                # Task functions are available for async actor tasks.
                print(ray.get(b.get_task_function_name.remote()))
                # Task functions are available for normal tasks.
                print(ray.get(get_task_function_name.remote()))

            .. testoutput::
                :options: +MOCK

                [python modual name].Actor.get_task_function_name
                [python modual name].AsyncActor.get_task_function_name
                [python modual name].get_task_function_name

        Returns:
            The current worker's task function call string
        r=   N)r   r!   r"   r#   r$   r>   r   current_task_function_namer6   s    r   get_task_function_namez%RuntimeContext.get_task_function_name  s\    V ;s|2>>>NN<)-)9< <   4{55r   zUse get_actor_id() insteadc                     | j         j        t          j        j         j        k    sJ d| j         j                     | j         j        }|                                s|ndS )zGet the current actor ID in this worker.

        ID of the actor of the current process.
        This shouldn't be used in a driver process.

        Returns:
            The current actor id in this worker. None if there's no actor id.
        r9   N)r   r!   r"   r#   r$   r    r)   r   r    s     r   r    zRuntimeContext.actor_idK  sg     K 3 ????;(,(8; ; @?? ;''00:xxd:r   c                    | j         j        t          j        j         j        k    r)t
                              d| j         j                    dS | j         j        }|                                s|	                                ndS )a6  Get the current actor ID in this worker.

        ID of the actor of the current process.
        This shouldn't be used in a driver process.
        The ID will be in hex format.

        Returns:
            The current actor id in hex format in this worker. None if there's no
            actor id.
        r=   N)
r   r!   r"   r#   r$   r>   debugr    r)   r-   rI   s     r   get_actor_idzRuntimeContext.get_actor_id^  s{     ;s|2>>>LL<)-)9< <   4;'%-__%6%6@x||~~~D@r   c                     | j         j        t          j        j         j        k    r)t
                              d| j         j                    dS | j         j        }|                                s| j         j	        ndS )a  Get the current actor name of this worker.

        This shouldn't be used in a driver process.
        The name is in string format.

        Returns:
            The current actor name of this worker.
            If a current worker is an actor, and
            if actor name doesn't exist, it returns an empty string.
            If a current worker is not an actor, it returns None.
        r=   N)
r   r!   r"   r#   r$   r>   r   r    r)   
actor_namerI   s     r   get_actor_namezRuntimeContext.get_actor_names  sy     ;s|2>>>NN<)-)9< <   4;'-5__->->Ht{%%DHr   c                     | j         j        S )zvGet the current namespace of this worker.

        Returns:
            The current namespace of this worker.
        )r   r   r6   s    r   r   zRuntimeContext.namespace  s     {$$r   c                     | j                                         r
J d            t          | j                                                   }|o|d         dk    S )zCheck whether this actor has been restarted.

        Returns:
            Whether this actor has been ever restarted.
        z0This method should't be called inside Ray tasks.)r    NumRestartsr   )r    r)   r   r-   )r   
actor_infos     r   was_current_actor_reconstructedz.RuntimeContext.was_current_actor_reconstructed  sb     $$&&	> 	>=	> 	>&T]%6%6%8%8999
<j71<<r   z$Use get_placement_group_id() insteadc                     | j         j        S )zGet the current Placement group ID of this worker.

        Returns:
            The current placement group id of this worker.
        )r   placement_group_idr6   s    r   current_placement_group_idz)RuntimeContext.current_placement_group_id  s     {--r   c                 n    | j         j        }|                                s|                                ndS )zGet the current Placement group ID of this worker.

        Returns:
            The current placement group id in hex format of this worker.
        N)r   rV   r)   r-   )r   pg_ids     r   get_placement_group_idz%RuntimeContext.get_placement_group_id  s-     ."',,..:uyy{{{d:r   c                     | j         j        S )a	  Get if the current task should capture parent's placement group.

        This returns True if it is called inside a driver.

        Returns:
            Return True if the current task should implicitly
                capture the parent placement group.
        )r   -should_capture_child_tasks_in_placement_groupr6   s    r   r\   z<RuntimeContext.should_capture_child_tasks_in_placement_group  s     {HHr   c                 D   | j         j        t          j        j         j        k    sJ d| j         j                     | j                                          | j         j                                        }d |                                D             }t          |          }|S )a  Get the assigned resources to this worker.

        By default for tasks, this will return {"CPU": 1}.
        By default for actors, this will return {}. This is because
        actors do not have CPUs assigned to them by default.

        Returns:
            A dictionary mapping the name of a resource to a float, where
            the float represents the amount of that resource reserved
            for this worker.
        r9   c                 H    i | ]\  }}|t          d  |D                        S )c              3       K   | ]	\  }}|V  
d S r    ).0_amts      r   	<genexpr>zCRuntimeContext.get_assigned_resources.<locals>.<dictcomp>.<genexpr>  s&      //VQS//////r   )sum)ra   resmappings      r   
<dictcomp>z9RuntimeContext.get_assigned_resources.<locals>.<dictcomp>  sE     
 
 
W //w/////
 
 
r   )
r   r!   r"   r#   r$   check_connectedcore_workerresource_idsitemsr	   )r   resource_id_mapresource_mapresults       r   get_assigned_resourcesz%RuntimeContext.get_assigned_resources  s     K 3 ????;(,(8; ; @?? 	##%%%+1>>@@
 
 / 5 5 7 7
 
 
 :,GGr   c                     | j         j        S )zGet the runtime env string used for the current driver or worker.

        Returns:
            The runtime env string currently using by this worker.
        )r   runtime_envr6   s    r   get_runtime_env_stringz%RuntimeContext.get_runtime_env_string  s     {&&r   c                 N    t          j        |                                           S )zGet the runtime env used for the current driver or worker.

        Returns:
            The runtime env currently using by this worker. The type of
                return value is ray.runtime_env.RuntimeEnv.
        )r   deserializers   r6   s    r   rr   zRuntimeContext.runtime_env  s!     %d&A&A&C&CDDDr   c                     | j         }|                                 |j        }|                                rt	          d          |j                            |          S )zvGet the current actor handle of this actor itself.

        Returns:
            The handle of current actor.
        z*This method is only available in an actor.)r   ri   r    r)   RuntimeErrorrj   get_actor_handle)r   r   r    s      r   current_actorzRuntimeContext.current_actor  s]        ??? 	MKLLL!228<<<r   c                 V    | j                                          | j         j        j        S )znGet the GCS address of the ray cluster.

        Returns:
            The GCS address of the cluster.
        )r   ri   
gcs_clientaddressr6   s    r   gcs_addresszRuntimeContext.gcs_address  s&     	##%%%{%--r   z!Use get_accelerator_ids() insteadc                 *    |                                  S r   )get_accelerator_idsr6   s    r   get_resource_idszRuntimeContext.get_resource_ids  s    '')))r   c                     | j         }|                                 i }t          j        j                                        D ]+}|                    |d| d          }d |D             ||<   ,|S )a  
        Get the current worker's visible accelerator ids.

        Returns:
            A dictionary keyed by the accelerator resource name. The values are a list
            of ids `{'GPU': ['0', '1'], 'neuron_cores': ['0', '1'],
            'TPU': ['0', '1']}`.
        ^z_group_[0-9A-Za-z]+$c                 ,    g | ]}t          |          S r`   )str)ra   ids     r   
<listcomp>z6RuntimeContext.get_accelerator_ids.<locals>.<listcomp>  s    2U2U2Ur3r772U2U2Ur   )r   ri   r"   r#   accelerators"get_all_accelerator_resource_names,get_accelerator_ids_for_accelerator_resource)r   r   ids_dictaccelerator_resource_nameaccelerator_idss        r   r   z"RuntimeContext.get_accelerator_ids  s        )+ \&IIKK	V 	V%$QQ)C-CCC O 3V2U_2U2U2UH.//r   c                 F    | j         }|                                 |j        S )z
        Get the node labels of the current worker.

        Returns:
            A dictionary of label key-value pairs.
        )r   ri   current_node_labelsr   s     r   get_node_labelszRuntimeContext.get_node_labels  s&        ))r   N)(__name__
__module____qualname____doc__r   r   r   r   r   r&   propertyr   r.   r   r3   r7   r   r   r?   r
   r:   rD   rG   r    rL   rO   r   rT   rW   rZ   r\   rp   rs   rr   ry   r}   r   r   r   r   r`   r   r   r   r      s       33   ZFPT  T#s(^    & Z2DAAA  BA XC    ( Z3TBBB  CB XS    (	+s 	+ 	+ 	+ 	+ Z3TBBB'9 '9 CB X'9R-?Xc] -? -? -? -?^+f + + + +8-x} 8- 8- 8- 8-t16 16 16 16 16f Z4dCCC; ; DC X;"Ahsm A A A A*I I I I I, % % X% 
= 
= X
= Z>MMM. . NM X.; ; ; ; ; 	I 	I X	I  2' ' ' E E XE = = X= . . X. Z;TJJJ*$sDI~"6 * * * KJ*T#tCy.%9    ,
*c49n!5 
* 
* 
* 
* 
* 
*r   r   r   c                      t           5  t          #t          t          j        j        j                  at          cddd           S # 1 swxY w Y   dS )a  Get the runtime context of the current driver/worker.

    The obtained runtime context can be used to get the metadata
    of the current driver, task, or actor.

    Example:

        .. testcode::

            import ray
            # Get the job id.
            ray.get_runtime_context().get_job_id()
            # Get the actor id.
            ray.get_runtime_context().get_actor_id()
            # Get the task id.
            ray.get_runtime_context().get_task_id()

    N)_runtime_context_lock_runtime_contextr   r"   r#   r   global_workerr`   r   r   get_runtime_contextr   .  s    * 
    #-cl.A.OPP                                   s   1AA
A
)logging	threadingtypingr   r   r   r   ray._private.workerr"   ray._private.client_mode_hookr   ray._private.stater   ray._private.utilsr	   ray._rayletr
   ray.runtime_envr   ray.util.annotationsr   r   	getLoggerr   r>   objectr   r   Lockr   r   r`   r   r   <module>r      sa        , , , , , , , , , , , ,     : : : : : : % % % % % % G G G G G G       & & & & & & 6 6 6 6 6 6 6 6		8	$	$ V* V* V* V* V*V V* V* V*r  &	((   ^             r   