o
    bi2                     @   s   d dl Z d dlZd dlmZ d dlmZ d dlmZ dZe j	
dddkatZe j	ddi d	Ze ad
t_dd ZdefddZdd Zdd Zdd Zedd Zedd ZdefddZdd Zdd  Zd!d" Zd#d$ ZdS )%    N)contextmanagerwraps)auto_init_ray__ray_client_mode_key__RAY_CLIENT_MODE01FTc                   C   s   t tdsdt_tjS )zGet's the value of `_client_hook_status_on_thread`.
    Since `_client_hook_status_on_thread` is a thread-local variable, we may
    need to add and set the 'status' attribute.
    statusT)hasattr_client_hook_status_on_threadr
    r   r   Q/home/ubuntu/.local/lib/python3.10/site-packages/ray/_private/client_mode_hook.py!_get_client_hook_status_on_thread   s   
r   valc                 C   s
   | t _d S N)r   r
   r   r   r   r   _set_client_hook_status'   s   
r   c                  C   s   t  } dt_| S NF)r   r   r
   )outr   r   r   _disable_client_hook,   s   r   c                   C   s   da dS )zkForce client mode to be enabled.
    NOTE: This should not be used in tests, use `enable_client_mode`.
    TNis_client_mode_enabledr   r   r   r   _explicitly_enable_client_mode3   s   r   c                   C   s   da d S r   r   r   r   r   r   _explicitly_disable_client_mode;   s   r   c                  c   s(    t  } z
d V  W t|  d S t|  w r   )r   r   r   r   r   r   disable_client_hook@   s
   r   c                   c   s$    t   z	d V  W t  d S t  w r   )r   r   r   r   r   r   enable_client_modeI   s
   r   funcc                    s&   ddl m t  fdd}|S )zDecorator for whether to use the 'regular' ray version of a function,
    or the Ray Client version of that function.

    Args:
        func: This function. This is set when this function is used
            as a decorator.
    r   rayc                     s8   t  r jdks
trt j| i |S  | i |S )Ninit)client_mode_should_convert__name__!is_client_mode_enabled_by_defaultgetattr)argskwargsr   r   r   r   wrapper]   s   z!client_mode_hook.<locals>.wrapper)ray.util.clientr   r   r   r(   r   r'   r   client_mode_hookR   s   	r+   c                   C   s   t ptot S )z;Determines if functions should be converted to client mode.)r   r#   r   r   r   r   r   r!   m   s   r!   c                    s   t   fdd}|S )a  Wraps a function called during client mode for execution as a remote
    task.

    Can be used to implement public features of ray client which do not
    belong in the main ray API (`ray.*`), yet require server-side execution.
    An example is the creation of placement groups:
    `ray.util.placement_group.placement_group()`. When called on the client
    side, this function is wrapped in a task to facilitate interaction with
    the GCS.
    c                     sP   ddl m} t  t r!|jdd }|j| i |}||S  | i |S )Nr   r   )num_cpus)r)   r   r   r!   remoteget)r%   r&   r   frefr   r   r   r(      s   
z!client_mode_wrap.<locals>.wrapperr   r*   r   r1   r   client_mode_wrapw   s   r2   c                 K   ^   ddl m} t| td}|du s||s || }t| t| ||}|j||fi |S )aX  Runs a preregistered ray RemoteFunction through the ray client.

    The common case for this is to transparently convert that RemoteFunction
    to a ClientRemoteFunction. This happens in circumstances where the
    RemoteFunction is declared early, in a library and only then is Ray used in
    client mode -- necessitating a conversion.
    r   r   N)	r)   r   r$   RAY_CLIENT_MODE_ATTR_converted_key_exists_convert_functionsetattr_get_converted_remote)func_clsin_args	in_kwargsr&   r   keyclient_funcr   r   r   client_mode_convert_function   s   

r?   c                 K   r3   )aN  Runs a preregistered actor class on the ray client

    The common case for this decorator is for instantiating an ActorClass
    transparently as a ClientActorClass. This happens in circumstances where
    the ActorClass is declared early, in a library and only then is Ray used in
    client mode -- necessitating a conversion.
    r   r   N)	r)   r   r$   r4   r5   _convert_actorr7   r8   r9   )	actor_clsr;   r<   r&   r   r=   client_actorr   r   r   client_mode_convert_actor   s   

rC   ) os	threading
contextlibr   	functoolsr   ray._private.auto_init_hookr   r4   environr.   r   r#   updateis_init_calledlocalr   r
   r   boolr   r   r   r   r   r   callabler+   r!   r2   r?   rC   r   r   r   r   <module>   s4    


