
    wi%                         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dS )    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dS )AsyncResultz4 Adapted from ``multiprocessing.pool.ApplyResult``. NTcachecallbackerror_callbackjob_iddelete_from_cachetimeoutreturnc                 2   || _         || _        || _        || _        || _        |t          t                    n|| _        t          j	                    | _
        d| _        d| _        | j        | j         v rt          d| d          | | j         | j        <   dS )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          j/var/www/development/aibuddy-work/election-extract/venv/lib/python3.11/site-packages/mpire/async_result.py__init__zAsyncResult.__init__   s     !-"3+1>d;'''v%O--;$+%%GvGGGHHH#'DK       c                 4    | j                                         S )z?
        :return: Returns True if the task is finished
        )r    is_setr$   s    r%   readyzAsyncResult.ready+   s      '')))r'   c                 f    |                                  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   s5    
 zz|| 	<:::;;;}r'   c                 :    | j                             |           dS )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                     |                      |           |                                 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   sF     			'zz|| 	= 	;+r'   successresultc                     || _         || _        | j        r!| j         r|                     | j                   | j        r!| j         s|                     | j                   | j                                         | j        r| j        | j        = dS dS )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      > 	(dm 	(NN4;''' 	. 	.  ---" 	)DK(((	) 	)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dS )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        <   ||                     |           dS dS )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>3C3CDDD'o//;$+%%GvGGGHHH#'DK OOG$$$$$ r'   c                     | S r:   rB   r*   s    r%   __iter__z%UnorderedAsyncResultIterator.__iter__   s    r'   Tblockc                    | j         r)| xj        dz  c_        | j                                         S | j        | j        | j        k    rt          |st
          j        | j        5  | j         sO| j                            |           }|rt
          j        | j        | j        | j        k    rt          | j         O| xj        dz  c_        | j                                         cddd           S # 1 swxY w Y   dS )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   sV    ; 	)!;&&(((=$)9T])J)J 	+ _ 		) 		)k ( $ 4 4W 4 E EE	 &+%=,1AT]1R1R'' k ( !;&&((		) 		) 		) 		) 		) 		) 		) 		) 		) 		) 		) 		) 		) 		) 		) 		) 		) 		)s   $A?C00C47C4c                     | j         5  | j        | j        | j        k     r0| j                                          | j         | j        | j        k     0ddd           dS # 1 swxY w Y   dS )z6
        Wait until all results are available
        N)rN   rH   rO   r/   r*   s    r%   r/   z!UnorderedAsyncResultIterator.wait   s     _ 	' 	'-'4+;dm+K+K$$&&& -'4+;dm+K+K	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	's   AAA!$A!r4   r5   c                    |re| xj         dz  c_         | j                            |           | j        5  | j                                         ddd           dS # 1 swxY w Y   dS || _        | j                                         dS )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   s      	&!Kv&&& ) )&&((() ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) %DO##%%%%%s   AA"Alengthc                     | j         '| j         |k    rt          d| j          d|           dS | j        5  || _         | j                                         ddd           dS # 1 swxY w Y   dS )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     =$}&&  "?t} "? "?6<"? "? @ @ @ F_ 	% 	%"DMO""$$$	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	%s   !A$$A(+A(c                 B    | 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                      | j         | j        = dS )z4
        Remove the iterator from the cache
        N)r   r   r*   s    r%   remove_from_cachez.UnorderedAsyncResultIterator.remove_from_cache   s     K$$$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   sE       k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                 V    t                                          |d d |dd            d S )NF)r   r   r   r   r   )superr&   )r$   r   r   	__class__s      r%   r&   z'AsyncResultWithExceptionGetter.__init__   s<    d6ej!% 	 	' 	' 	' 	' 	'r'   c                 8    |                                   | j        S re   )r/   r"   r*   s    r%   rf   z,AsyncResultWithExceptionGetter.get_exception   s     			{r'   c                 T    d| _         d| _        | j                                         dS ))
        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                   H     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                 \    t                                          |d t          d            d S )N)rE   r   r   )ro   r&   r	   )r$   r   rp   s     r%   r&   z)UnorderedAsyncExitResultIterator.__init__   s*    YMMMMMr'   c                 *    t          | j                  S )z/
        :return: List of exit results
        )listrK   r*   s    r%   get_resultsz,UnorderedAsyncExitResultIterator.get_results   s     DK   r'   c                     d| _         | j                                         d| _        d| _        d| _        | j                                         dS )rs   Nr   )rH   rK   rt   rO   rP   rQ   rR   r*   s    r%   ru   z&UnorderedAsyncExitResultIterator.reset  sQ     !!#####r'   ri   )
r;   r<   r=   r   r&   r   r   r}   ru   rv   rw   s   @r%   ry   ry      s        Nd Nt N N N N N 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      s`                = = = = = = = = = = = = = = = = , , , , , , , ,ioZ) Z) Z) Z) Z) Z) Z) Z)zx% x% x% x% x% x% x% x%v" " " " "[ " " ",$ $ $ $ $'C $ $ $. %CEa89 :r'   