
    4 j%                         d dl Z d dlZd dlZd dlZd dlmZmZmZmZm	Z	m
Z
 d dlmZmZ  ej                         Z G d d      Z G d d      Z G d d	e      Z G d
 de      Ze
eeeef   Zy)    N)AnyCallableDictListOptionalUnion)	EXIT_FUNC	INIT_FUNCc                       e Zd ZdZ	 ddedee   dee   dee   dedee	   d	dfd
Z
d	efdZd	efdZddee	   d	dfdZddee	   d	efdZdeded	dfdZy)AsyncResultz4 Adapted from ``multiprocessing.pool.ApplyResult``. Ncachecallbackerror_callbackjob_iddelete_from_cachetimeoutreturnc                 H   || _         || _        || _        || _        || _        |t        t              n|| _        t        j                         | _
        d| _        d| _        | j                  | j                   v rt        d| d      | | j                   | j                  <   y)a  
        :param cache: Cache for storing intermediate results
        :param callback: Callback function to call when the task is finished. The callback function receives the output
            of the function as its argument
        :param error_callback: Callback function to call when the task has failed. The callback function receives the
            exception as its argument
        :param job_id: Job ID of the task. If None, a new job ID is generated
        :param delete_from_cache: If True, the result is deleted from the cache when the task is finished
        :param timeout: Timeout in seconds for a single task. When the timeout is exceeded, MPIRE will raise a
            ``TimeoutError``. Use ``None`` to disable (default)
        NJob ID  already exists in cache)_cache	_callback_error_callback_delete_from_cache_timeoutnextjob_counterr   	threadingEvent_ready_event_success_value
ValueError)selfr   r   r   r   r   r   s          E/home/agent/.local/lib/python3.12/site-packages/mpire/async_result.py__init__zAsyncResult.__init__   s     !-"3+1>d;'v%OO-;;$++%wvh.FGHH#'DKK     c                 6    | j                   j                         S )z?
        :return: Returns True if the task is finished
        )r    is_setr$   s    r%   readyzAsyncResult.ready+   s       ''))r'   c                 j    | j                         st        | j                   d      | j                  S )z
        :return: Returns True if the task has finished successfully
        :raises: ValueError if the task is not finished yet
        z is not ready)r+   r#   r   r!   r*   s    r%   
successfulzAsyncResult.successful1   s-    
 zz|}M:;;}}r'   c                 :    | j                   j                  |       y)zy
        Wait until the task is finished

        :param timeout: Timeout in seconds. If None, wait indefinitely
        N)r    waitr$   r   s     r%   r/   zAsyncResult.wait:   s     	w'r'   c                     | j                  |       | j                         st        | j                  r| j                  S | j                  )am  
        Wait until the task is finished and return the output of the function

        :param timeout: Timeout in seconds. If None, wait indefinitely
        :return: Output of the function
        :raises: TimeoutError if the task is not finished within the timeout. When the task has failed, the exception
            raised by the function is re-raised
        )r/   r+   TimeoutErrorr!   r"   r0   s     r%   getzAsyncResult.getB   s:     			'zz|==;;++r'   successresultc                 h   || _         || _        | j                  r'| j                   r| j                  | j                         | j                  r'| j                   s| j                  | j                         | j                  j                          | j                  r| j                  | j                  = yy)aw  
        Set the result of the task and call any callbacks, when provided. This also removes the task from the cache, as
        it's no longer needed there. The user should store a reference to the result object

        :param success: True if the task has finished successfully
        :param result: Output of the function or the exception raised by the function
        N)	r!   r"   r   r   r    setr   r   r   r$   r4   r5   s      r%   _setzAsyncResult._setS   s      >>dmmNN4;;'  -""DKK( #r'   )NTNN)__name__
__module____qualname____doc__r   r   r   intboolfloatr&   r+   r-   r/   r   r3   r9    r'   r%   r   r      s    > ko(d (hx.@ (RZ[cRd (!#(BF(X`afXg(sw(6*t *D (HUO (t (8E? c ")D )# )$ )r'   r   c                       e Zd ZdZ	 	 ddedee   dee   dee   ddf
dZdd	Z	dd
e
dee   defdZeZddZde
deddfdZdeddfdZdefdZddZy)UnorderedAsyncResultIteratorza Stores results of a task and provides an iterator to obtain the results in an unordered fashion Nr   n_tasksr   r   r   c                    || _         d| _        || _        |t        t              n|| _        t        j                         | _        t        j                  t        j                               | _        d| _        d| _        d| _        t        j                          | _        | j
                  | j                   v rt%        d| d      | | j                   | j
                  <   || j'                  |       yy)a  
        :param cache: Cache for storing intermediate results
        :param n_tasks: Number of tasks that will be executed. If None, we don't know the lenght yet
        :param job_id: Job ID of the task. If None, a new job ID is generated
        :param timeout: Timeout in seconds for a single task. When the timeout is exceeded, MPIRE will raise a
            ``TimeoutError``. Use ``None`` to disable (default)
        N)lockr   r   r   )r   _n_tasksr   r   r   r   collectionsdeque_itemsr   	ConditionLock
_condition_n_received_n_returned
_exceptionr   _got_exceptionr#   
set_length)r$   r   rE   r   r   s        r%   r&   z%UnorderedAsyncResultIterator.__init__m   s     +1>d;'v!'')#--9>>3CD'oo/;;$++%wvh.FGHH#'DKK OOG$ r'   c                     | S r:   rB   r*   s    r%   __iter__z%UnorderedAsyncResultIterator.__iter__   s    r'   blockc                 z   | j                   r/| xj                  dz  c_        | j                   j                         S | j                  | j                  | j                  k(  rt        |st
        j                  | j                  5  | j                   sg| j                  j                  |       }|rt
        j                  | j                  | j                  | j                  k(  rt        | j                   sg| xj                  dz  c_        | j                   j                         cddd       S # 1 sw Y   yxY w)a2  
        Obtain the next unordered result for the task

        :param block: If True, wait until the next result is available. If False, raise queue.Empty if no result is
            available
        :param timeout: Timeout in seconds. If None, wait indefinitely
        :return: The next result
           N)r   )	rK   rP   popleftrH   StopIterationqueueEmptyrN   r/   )r$   rV   r   	timed_outs       r%   r   z!UnorderedAsyncResultIterator.next   s     ;;!;;&&((==$)9)9T]])J++ __ 		)kk $ 4 4W 4 EE	++%==,1A1AT]]1R'' kk !;;&&(		) 		) 		)s   A3D19.D11D:c                    | j                   5  | j                  | j                  | j                  k  rA| j                   j                          | j                  '| j                  | j                  k  rAddd       y# 1 sw Y   yxY w)z6
        Wait until all results are available
        N)rN   rH   rO   r/   r*   s    r%   r/   z!UnorderedAsyncResultIterator.wait   sh     __ 	'--'4+;+;dmm+K$$& --'4+;+;dmm+K	' 	' 	's   AA=A==Br4   r5   c                 "   |r`| xj                   dz  c_         | j                  j                  |       | j                  5  | j                  j	                          ddd       y|| _        | j                  j                          y# 1 sw Y   yxY w)z
        Set the result of the task

        :param success: True if the task has finished successfully
        :param result: Output of the function or the exception raised by the function
        rX   N)rO   rK   appendrN   notifyrQ   rR   r7   r8   s      r%   r9   z!UnorderedAsyncResultIterator._set   sv     !KKv& )&&() ) %DO##%	) )s   BBlengthc                     | j                   +| j                   |k7  rt        d| j                    d|       y| j                  5  || _         | j                  j                          ddd       y# 1 sw Y   yxY w)z_
        Set the length of the iterator

        :param length: Length of the iterator
        Nz+Length of iterator has already been set to z, but is now set to )rH   r#   rN   ra   )r$   rb   s     r%   rS   z'UnorderedAsyncResultIterator.set_length   s|     ==$}}& #Nt}}o ^66<X"? @ @ __ 	%"DMOO""$	% 	% 	%s   "A//A8c                 N    | j                   j                          | j                  S z?
        :return: The exception raised by the function
        )rR   r/   rQ   r*   s    r%   get_exceptionz*UnorderedAsyncResultIterator.get_exception   s      	  "r'   c                 2    | j                   | j                  = y)z4
        Remove the iterator from the cache
        N)r   r   r*   s    r%   remove_from_cachez.UnorderedAsyncResultIterator.remove_from_cache   s     KK$r'   )NN)r   rD   )TNr   N)r;   r<   r=   r>   r   r   r?   rA   r&   rU   r@   r   r   __next__r/   r9   rS   	Exceptionrf   rh   rB   r'   r%   rD   rD   i   s    kTX,0%d %Xc] %HSM %"5/%59%6)$ ) )3 )> H'&D &# &$ &"% % %"y %r'   rD   c                   @     e Zd Zdededdf fdZdefdZddZ xZ	S )	AsyncResultWithExceptionGetterr   r   r   Nc                 0    t         |   |d d |dd        y )NF)r   r   r   r   r   )superr&   )r$   r   r   	__class__s      r%   r&   z'AsyncResultWithExceptionGetter.__init__   s#    d6ej!% 	 	'r'   c                 :    | j                          | j                  S re   )r/   r"   r*   s    r%   rf   z,AsyncResultWithExceptionGetter.get_exception   s     			{{r'   c                 T    d| _         d| _        | j                  j                          y))
        Reset the result object
        N)r!   r"   r    clearr*   s    r%   resetz$AsyncResultWithExceptionGetter.reset   s$     !r'   ri   )
r;   r<   r=   r   r?   r&   rk   rf   ru   __classcell__rp   s   @r%   rm   rm      s.    'd 'C 'D 'y "r'   rm   c                   B     e Zd Zdeddf fdZdee   fdZddZ xZ	S ) UnorderedAsyncExitResultIteratorr   r   Nc                 4    t         |   |d t        d        y )N)rE   r   r   )ro   r&   r	   )r$   r   rp   s     r%   r&   z)UnorderedAsyncExitResultIterator.__init__   s    YMr'   c                 ,    t        | j                        S )z/
        :return: List of exit results
        )listrK   r*   s    r%   get_resultsz,UnorderedAsyncExitResultIterator.get_results   s     DKK  r'   c                     d| _         | j                  j                          d| _        d| _        d| _        | j                  j                          y)rs   Nr   )rH   rK   rt   rO   rP   rQ   rR   r*   s    r%   ru   z&UnorderedAsyncExitResultIterator.reset  sD     !!#r'   ri   )
r;   r<   r=   r   r&   r   r   r}   ru   rv   rw   s   @r%   ry   ry      s.    Nd Nt N!T#Y !	$r'   ry   )rI   	itertoolsr[   r   typingr   r   r   r   r   r   mpire.commsr	   r
   countr   r   rD   rm   ry   AsyncResultTyperB   r'   r%   <module>r      sw        = = ,iooZ) Z)zx% x%v"[ ",$'C $. %CEa89 :r'   