
    &`i8                         d dl mZmZmZmZmZ d dlZd dlmZ erd dl	Z ed          Z
e G d d                      ZdS )    )TYPE_CHECKINGAnyCallableListTypeVarN)DeveloperAPIVc                       e Zd ZdZdefdZdedegef         de	e         fdZ
dedegef         de	e         fdZd	 Zd
 ZddZddZd Zd Zd Zd ZdS )	ActorPoolar  Utility class to operate on a fixed pool of actors.

    Arguments:
        actors: List of Ray actor handles to use in this pool.

    Examples:
        .. testcode::

            import ray
            from ray.util.actor_pool import ActorPool

            @ray.remote
            class Actor:
                def double(self, v):
                    return 2 * v

            a1, a2 = Actor.remote(), Actor.remote()
            pool = ActorPool([a1, a2])
            print(list(pool.map(lambda a, v: a.double.remote(v),
                                [1, 2, 3, 4])))

        .. testoutput::

            [2, 4, 6, 8]
    actorsc                     ddl m}  |d           t          |          | _        i | _        i | _        d| _        d| _        g | _        d S )Nr   )record_library_usagezutil.ActorPool)	ray._common.usage.usage_libr   list_idle_actors_future_to_actor_index_to_future_next_task_index_next_return_index_pending_submits)selfr   r   s      g/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/ray/util/actor_pool.py__init__zActorPool.__init__(   sn    DDDDDD-... !LL !# !# !" #$ !#    fnzray.actor.ActorHandlevaluesc                                                        r=	                      dd           n# t          $ r Y nw xY w                                  =|D ]}                     ||            fd} |            S )a  Apply the given function in parallel over the actors and values.

        This returns an ordered iterator that will return results of the map
        as they finish. Note that you must iterate over the iterator to force
        the computation to finish.

        Arguments:
            fn: Function that takes (actor, value) as argument and
                returns an ObjectRef computing the result over the value. The
                actor will be considered busy until the ObjectRef completes.
            values: List of values that fn(actor, value) should be
                applied to.

        Returns:
            Iterator over results from applying fn to the actors and values.

        Examples:
            .. testcode::

                import ray
                from ray.util.actor_pool import ActorPool

                @ray.remote
                class Actor:
                    def double(self, v):
                        return 2 * v

                a1, a2 = Actor.remote(), Actor.remote()
                pool = ActorPool([a1, a2])
                print(list(pool.map(lambda a, v: a.double.remote(v),
                                    [1, 2, 3, 4])))

            .. testoutput::

                [2, 4, 6, 8]
        r   T)timeoutignore_if_timedoutc               3      K                                     r,                                 V                                    *d S d S N)has_nextget_nextr   s   r   get_generatorz$ActorPool.map.<locals>.get_generatoro   sQ      --// &mmoo%%% --// & & & & &r   )r"   r#   TimeoutErrorsubmitr   r   r   vr%   s   `    r   mapzActorPool.map?   s    N mmoo 	aDAAAA    mmoo 	  	 	AKKA	& 	& 	& 	& 	& }s   / 
<<c                                                        r<	                      d           n# t          $ r Y nw xY w                                  <|D ]}                     ||            fd} |            S )a  Similar to map(), but returning an unordered iterator.

        This returns an unordered iterator that will return results of the map
        as they finish. This can be more efficient that map() if some results
        take longer to compute than others.

        Arguments:
            fn: Function that takes (actor, value) as argument and
                returns an ObjectRef computing the result over the value. The
                actor will be considered busy until the ObjectRef completes.
            values: List of values that fn(actor, value) should be
                applied to.

        Returns:
            Iterator over results from applying fn to the actors and values.

        Examples:
            .. testcode::

                import ray
                from ray.util.actor_pool import ActorPool

                @ray.remote
                class Actor:
                    def double(self, v):
                        return 2 * v

                a1, a2 = Actor.remote(), Actor.remote()
                pool = ActorPool([a1, a2])
                print(list(pool.map_unordered(lambda a, v: a.double.remote(v),
                                              [1, 2, 3, 4])))

            .. testoutput::
                :options: +MOCK

                [6, 8, 4, 2]
        r   r   c               3      K                                     r,                                 V                                    *d S d S r!   )r"   get_next_unorderedr$   s   r   r%   z.ActorPool.map_unordered.<locals>.get_generator   sU      --// 0--///// --// 0 0 0 0 0r   )r"   r.   r&   r'   r(   s   `    r   map_unorderedzActorPool.map_unorderedu   s    T mmoo 	'''2222    mmoo 	  	 	AKKA	0 	0 	0 	0 	0 }s   . 
;;c                 F   | j         r}| j                                         } |||          }t          |t                    rt	          |          n|}| j        |f| j        |<   || j        | j        <   | xj        dz  c_        dS | j        	                    ||f           dS )av  Schedule a single task to run in the pool.

        This has the same argument semantics as map(), but takes on a single
        value instead of a list of values. The result can be retrieved using
        get_next() / get_next_unordered().

        Arguments:
            fn: Function that takes (actor, value) as argument and
                returns an ObjectRef computing the result over the value. The
                actor will be considered busy until the ObjectRef completes.
            value: Value to compute a result for.

        Examples:
            .. testcode::

                import ray
                from ray.util.actor_pool import ActorPool

                @ray.remote
                class Actor:
                    def double(self, v):
                        return 2 * v

                a1, a2 = Actor.remote(), Actor.remote()
                pool = ActorPool([a1, a2])
                pool.submit(lambda a, v: a.double.remote(v), 1)
                pool.submit(lambda a, v: a.double.remote(v), 2)
                print(pool.get_next(), pool.get_next())

            .. testoutput::

                2 4
           N)
r   pop
isinstancer   tupler   r   r   r   append)r   r   valueactorfuture
future_keys         r   r'   zActorPool.submit   s    D  	6%))++ERu%%F*4VT*B*BNvJ151F0ND!*-;AD!$"78!!Q&!!!!!(("e55555r   c                 *    t          | j                  S )a  Returns whether there are any pending results to return.

        Returns:
            True if there are any pending results not yet returned.

        Examples:
            .. testcode::

                import ray
                from ray.util.actor_pool import ActorPool

                @ray.remote
                class Actor:
                    def double(self, v):
                        return 2 * v

                a1, a2 = Actor.remote(), Actor.remote()
                pool = ActorPool([a1, a2])
                pool.submit(lambda a, v: a.double.remote(v), 1)
                print(pool.has_next())
                print(pool.get_next())
                print(pool.has_next())

            .. testoutput::

                True
                2
                False
        )boolr   r$   s    r   r"   zActorPool.has_next   s    < D)***r   NFc                 t   |                                  st          d          | j        | j        k    rt	          d          | j        | j                 }d}d}|/t          j        |g|          \  }}|s|st          |          d}| j        | j        = | xj        dz  c_        t          |t                    rt          |          n|}| j                            |          \  }	}
|                     |
           |r%t          |d	                    |          z             t          j        |          S )
a1  Returns the next pending result in order.

        This returns the next result produced by submit(), blocking for up to
        the specified timeout until it is available.

        Returns:
            The next result.

        Raises:
            TimeoutError: if the timeout is reached.

        Examples:
            .. testcode::

                import ray
                from ray.util.actor_pool import ActorPool

                @ray.remote
                class Actor:
                    def double(self, v):
                        return 2 * v

                a1, a2 = Actor.remote(), Actor.remote()
                pool = ActorPool([a1, a2])
                pool.submit(lambda a, v: a.double.remote(v), 1)
                print(pool.get_next())

            .. testoutput::

                2
        No more results to getz@It is not allowed to call get_next() after get_next_unordered().Timed out waiting for resultFNr,   Tr1   . The task {} has been ignored.)r"   StopIterationr   r   
ValueErrorr   raywaitr&   r3   r   r4   r   r2   _return_actorformatget)r   r   r   r8   timeout_msgraise_timeout_after_ignoreres_r9   ias              r   r#   zActorPool.get_next   sZ   @ }} 	: 8999"d&;;;R   &t'>?4%*"Xvh888FC 6) 6&{33315.!$"9:1$&0&>&>JU6]]]F
$((4411% 	?FFvNNN   wvr   c                     |                                  st          d          t          j        t	          | j                  d|          \  }}d}d}|r|\  }n|st          |          d}| j                            |          \  }}	|                     |	           | j	        |= t          | j        |dz             | _        |r%t          |d                    |          z             t          j        |          S )a[  Returns any of the next pending results.

        This returns some result produced by submit(), blocking for up to
        the specified timeout until it is available. Unlike get_next(), the
        results are not always returned in same order as submitted, which can
        improve performance.

        Returns:
            The next result.

        Raises:
            TimeoutError: if the timeout is reached.

        Examples:
            .. testcode::

                import ray
                from ray.util.actor_pool import ActorPool

                @ray.remote
                class Actor:
                    def double(self, v):
                        return 2 * v

                a1, a2 = Actor.remote(), Actor.remote()
                pool = ActorPool([a1, a2])
                pool.submit(lambda a, v: a.double.remote(v), 1)
                pool.submit(lambda a, v: a.double.remote(v), 2)
                print(pool.get_next_unordered())
                print(pool.get_next_unordered())

            .. testoutput::
                :options: +MOCK

                4
                2
        r=   r1   )num_returnsr   r>   FTr?   )r"   r@   rB   rC   r   r   r&   r2   rD   r   maxr   rE   rF   )
r   r   r   rI   rJ   rG   rH   r8   rK   rL   s
             r   r.   zActorPool.get_next_unordered7  s   L }} 	: 8999$t4551gVVVQ4%*" 	2HVV% 2";///-1*$((0011!!$"%d&=q1u"E"E% 	?FFvNNN   wvr   c                     | j                             |           | j        r$ | j        | j                            d            d S d S )Nr   )r   r5   r   r'   r2   )r   r7   s     r   rD   zActorPool._return_actort  sT      '''  	7DK.221556666	7 	7r   c                 b    t          | j                  dk    ot          | j                  dk    S )a  Returns whether there are any idle actors available.

        Returns:
            True if there are any idle actors and no pending submits.

        Examples:
            .. testcode::

                import ray
                from ray.util.actor_pool import ActorPool

                @ray.remote
                class Actor:
                    def double(self, v):
                        return 2 * v

                a1 = Actor.remote()
                pool = ActorPool([a1])
                pool.submit(lambda a, v: a.double.remote(v), 1)
                print(pool.has_free())
                print(pool.get_next())
                print(pool.has_free())

            .. testoutput::

                False
                2
                True
        r   )lenr   r   r$   s    r   has_freezActorPool.has_freey  s/    < 4$%%)Mc$2G.H.HA.MMr   c                 `    |                                  r| j                                        S dS )a  Removes an idle actor from the pool.

        Returns:
            An idle actor if one is available.
            None if no actor was free to be removed.

        Examples:
            .. testcode::

                import ray
                from ray.util.actor_pool import ActorPool

                @ray.remote
                class Actor:
                    def double(self, v):
                        return 2 * v

                a1 = Actor.remote()
                pool = ActorPool([a1])
                pool.submit(lambda a, v: a.double.remote(v), 1)
                assert pool.pop_idle() is None
                assert pool.get_next() == 2
                assert pool.pop_idle() == a1

        N)rS   r   r2   r$   s    r   pop_idlezActorPool.pop_idle  s.    4 ==?? 	+$((***tr   c                     g }| j                                         r#t          | j                                          \  }}|| j        v s||v rt	          d          |                     |           dS )a  Pushes a new actor into the current list of idle actors.

        Examples:
            .. testcode::

                import ray
                from ray.util.actor_pool import ActorPool

                @ray.remote
                class Actor:
                    def double(self, v):
                        return 2 * v

                a1, a2 = Actor.remote(), Actor.remote()
                pool = ActorPool([a1])
                pool.push(a2)
        z*Actor already belongs to current ActorPoolN)r   r   zipr   rA   rD   )r   r7   busy_actorsrJ   s       r   pushzActorPool.push  s    $  '')) 	B $"7">">"@"@ANA{D%%%+)=)=IJJJu%%%%%r   )NF)__name__
__module____qualname____doc__r   r   r   r	   r   r   r*   r/   r'   r"   r#   r.   rD   rS   rU   rY    r   r   r   r      s4        4#t # # # #.4h 7;S@A 44PQ7 4 4 4 4l73Q7<=7GKAw7 7 7 7r*6 *6 *6X+ + +@; ; ; ;z; ; ; ;z7 7 7
N N N@  <& & & & &r   r   )typingr   r   r   r   r   rB   ray.util.annotationsr   	ray.actorr	   r   r^   r   r   <module>rb      s    > > > > > > > > > > > > > > 



 - - - - - - GCLL B& B& B& B& B& B& B& B& B& B&r   