o
    tBh[                  
   @   s  d dl Z d dlZd dlZd dlZd dlZe jdd dkr\d dlmZmZ d dlm	Z	m
Z
 d dlmZmZ d dlmZ d dlmZmZ d d	lmZ d d
lmZmZmZ d dlmZmZ ndZdZdZdZdZdZdZdZdZeeeeegZededededediZedZG dd deZG dd deZ
G dd deZ	G d d! d!eZG d"d# d#eZ G d$d% d%eZ!G d&d' d'eZ"G d(d) d)eZ#d*d+ Z$d8d,d-Ze%d.d/Z&defd0d1ZG d2d3 d3eZG d4d5 d5eZG d6d7 d7eZdS )9    N   )   r   )waitas_completed)TimeoutErrorCancelledError)ExecutorFuture)FIRST_EXCEPTION)ALL_COMPLETEDFIRST_COMPLETED)LOGGER)PENDINGRUNNING	CANCELLED)CANCELLED_AND_NOTIFIEDFINISHEDr   r
   r   _AS_COMPLETEDr   r   r   r   r   pendingrunning	cancelledfinishedzconcurrent.futuresc                   @      e Zd ZdZdS )Errorz-Base class for all future-related exceptions.N__name__
__module____qualname____doc__ r   r   r/var/www/html/riverr-enterprise-integrations-main/venv/lib/python3.10/site-packages/joblib/externals/loky/_base.pyr   C       r   c                   @   r   )r   zThe Future was cancelled.Nr   r   r   r   r    r   G   r!   r   c                   @   r   )r   z*The operation exceeded the given deadline.Nr   r   r   r   r    r   K   r!   r   c                   @   s0   e Zd ZdZdd Zdd Zdd Zdd	 Zd
S )_Waiterz;Provides the event that wait() and as_completed() block on.c                 C   s   t  | _g | _d S N)	threadingEventeventfinished_futuresselfr   r   r    __init__Q   s   

z_Waiter.__init__c                 C      | j | d S r#   r'   appendr)   futurer   r   r    
add_resultU      z_Waiter.add_resultc                 C   r+   r#   r,   r.   r   r   r    add_exceptionX   r1   z_Waiter.add_exceptionc                 C   r+   r#   r,   r.   r   r   r    add_cancelled[   r1   z_Waiter.add_cancelledN)r   r   r   r   r*   r0   r2   r3   r   r   r   r    r"   O   s    r"   c                       sD   e Zd ZdZ fddZ fddZ fddZ fdd	Z  ZS )
_AsCompletedWaiterzUsed by as_completed().c                    s   t t|   t | _d S r#   )superr4   r*   r$   Locklockr(   	__class__r   r    r*   a   s   z_AsCompletedWaiter.__init__c                    F   | j  tt| | | j  W d    d S 1 sw   Y  d S r#   )r7   r5   r4   r0   r&   setr.   r8   r   r    r0   e      "z_AsCompletedWaiter.add_resultc                    r:   r#   )r7   r5   r4   r2   r&   r;   r.   r8   r   r    r2   j   r<   z _AsCompletedWaiter.add_exceptionc                    r:   r#   )r7   r5   r4   r3   r&   r;   r.   r8   r   r    r3   o   r<   z _AsCompletedWaiter.add_cancelled)	r   r   r   r   r*   r0   r2   r3   __classcell__r   r   r8   r    r4   ^   s    r4   c                       s8   e Zd ZdZ fddZ fddZ fddZ  ZS )_FirstCompletedWaiterz*Used by wait(return_when=FIRST_COMPLETED).c                       t t| | | j  d S r#   )r5   r>   r0   r&   r;   r.   r8   r   r    r0   w      z _FirstCompletedWaiter.add_resultc                    r?   r#   )r5   r>   r2   r&   r;   r.   r8   r   r    r2   {   r@   z#_FirstCompletedWaiter.add_exceptionc                    r?   r#   )r5   r>   r3   r&   r;   r.   r8   r   r    r3      r@   z#_FirstCompletedWaiter.add_cancelled)r   r   r   r   r0   r2   r3   r=   r   r   r8   r    r>   t   s
    r>   c                       sL   e Zd ZdZ fddZdd Z fddZ fdd	Z fd
dZ  Z	S )_AllCompletedWaiterz<Used by wait(return_when=FIRST_EXCEPTION and ALL_COMPLETED).c                    s(   || _ || _t | _tt|   d S r#   )num_pending_callsstop_on_exceptionr$   r6   r7   r5   rA   r*   )r)   rB   rC   r8   r   r    r*      s   
z_AllCompletedWaiter.__init__c                 C   sZ   | j   |  jd8  _| js| j  W d    d S W d    d S 1 s&w   Y  d S )N   )r7   rB   r&   r;   r(   r   r   r    _decrement_pending_calls   s   "z,_AllCompletedWaiter._decrement_pending_callsc                       t t| | |   d S r#   )r5   rA   r0   rE   r.   r8   r   r    r0         z_AllCompletedWaiter.add_resultc                    s0   t t| | | jr| j  d S |   d S r#   )r5   rA   r2   rC   r&   r;   rE   r.   r8   r   r    r2      s   z!_AllCompletedWaiter.add_exceptionc                    rF   r#   )r5   rA   r3   rE   r.   r8   r   r    r3      rG   z!_AllCompletedWaiter.add_cancelled)
r   r   r   r   r*   rE   r0   r2   r3   r=   r   r   r8   r    rA      s    rA   c                   @   s(   e Zd ZdZdd Zdd Zdd ZdS )	_AcquireFutureszMA context manager that does an ordered acquire of Future conditions.
        c                 C   s   t |td| _d S )N)key)sortedidfutures)r)   rL   r   r   r    r*      s   z_AcquireFutures.__init__c                 C   s   | j D ]}|j  qd S r#   )rL   
_conditionacquirer.   r   r   r    	__enter__      
z_AcquireFutures.__enter__c                 G   s   | j D ]}|j  qd S r#   )rL   rM   release)r)   argsr/   r   r   r    __exit__   rP   z_AcquireFutures.__exit__N)r   r   r   r   r*   rO   rS   r   r   r   r    rH      s
    rH   c                 C   s   |t krt }n-|tkrt }n%tdd | D }|tkr$t|dd}n|tkr/t|dd}ntd| | D ]}|j	
| q7|S )Nc                 s   s    | ]
}|j ttfvV  qd S r#   _stater   r   .0fr   r   r    	<genexpr>   s
    
z._create_and_install_waiters.<locals>.<genexpr>T)rC   FzInvalid return condition: %r)r   r4   r   r>   sumr
   rA   r   
ValueError_waitersr-   )fsreturn_whenwaiterpending_countrX   r   r   r    _create_and_install_waiters   s&   ra   c           	      c   s   |dur|t    }t| } t|  tdd | D }| | }t| t}W d   n1 s0w   Y  z{|D ]}|V  q8|r|du rGd}n|t    }|dk r]tdt|t| f |j| |j	 |j
}g |_
|j  W d   n1 s|w   Y  |D ]
}|V  || q|s@W | D ]}|j |j| W d   n1 sw   Y  qdS | D ]}|j |j| W d   n1 sw   Y  qw )a  An iterator over the given futures that yields each as it completes.

        Args:
            fs: The sequence of Futures (possibly created by different
                Executors) to iterate over.
            timeout: The maximum number of seconds to wait. If None, then there
                is no limit on the wait time.

        Returns:
            An iterator that yields the given Futures as they complete
            (finished or cancelled). If any given Futures are duplicated, they
            will be returned once.

        Raises:
            TimeoutError: If the entire result iterator could not be generated
                before the given timeout.
        Nc                 s   "    | ]}|j ttfv r|V  qd S r#   rT   rV   r   r   r    rY      s    zas_completed.<locals>.<genexpr>r   z%d (of %d) futures unfinished)timer;   rH   ra   r   r   lenr&   r   r7   r'   clearremoverM   r\   )	r]   timeoutend_timer   r   r_   r/   wait_timeoutrX   r   r   r    r      sX   
r   DoneAndNotDoneFutureszdone not_donec              	   C   s8  t | ] tdd | D }t| | }|tkr&|r&t||W  d   S |tkrA|rAtdd |D rAt||W  d   S t|t| krUt||W  d   S t| |}W d   n1 sdw   Y  |j	| | D ]}|j
 |j| W d   n1 sw   Y  qq||j t|t| | S )a  Wait for the futures in the given sequence to complete.

        Args:
            fs: The sequence of Futures (possibly created by different
                Executors) to wait upon.
            timeout: The maximum number of seconds to wait. If None, then there
                is no limit on the wait time.
            return_when: Indicates when this function should return. The
                options are:

                FIRST_COMPLETED - Return when any future finishes or is
                                cancelled.
                FIRST_EXCEPTION - Return when any future finishes by raising an
                                exception. If no future raises an exception
                                then it is equivalent to ALL_COMPLETED.
                ALL_COMPLETED -   Return when all futures finish or are
                                cancelled.

        Returns:
            A named 2-tuple of sets. The first set, named 'done', contains the
            futures that completed (is finished or cancelled) before the wait
            completed. The second set, named 'not_done', contains uncompleted
            futures.
        c                 s   rb   r#   rT   rV   r   r   r    rY     s    zwait.<locals>.<genexpr>Nc                 s   s(    | ]}|  s| d ur|V  qd S r#   )r   	exceptionrV   r   r   r    rY   &  s    )rH   r;   r   rj   r
   anyrd   ra   r&   r   rM   r\   rf   updater'   )r]   rg   r^   donenot_doner_   rX   r   r   r    r     s.   
r   c                   @   s|   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d Zdd Z	dd Z
dd ZdddZdddZdd Zdd Zdd ZdS )_BaseFuturez5Represents the result of an asynchronous computation.c                 C   s,   t  | _t| _d| _d| _g | _g | _dS )z8Initializes the future. Should not be called by clients.N)	r$   	ConditionrM   r   rU   _result
_exceptionr\   _done_callbacksr(   r   r   r    r*   :  s   

z_BaseFuture.__init__c                 C   s   | j P | jtkr>| jr%d| jjt| t| j | jjjf W  d    S d| jjt| t| j | jjjf W  d    S d| jjt| t| j f W  d    S 1 sVw   Y  d S )Nz<%s at %#x state=%s raised %s>z <%s at %#x state=%s returned %s>z<%s at %#x state=%s>)	rM   rU   r   rs   r9   r   rK   _STATE_TO_DESCRIPTION_MAPrr   r(   r   r   r    __repr__C  s.   
	$z_BaseFuture.__repr__c                 C   s   | j 0 | jttfv r	 W d   dS | jttfv r$	 W d   dS t| _| j   W d   n1 s6w   Y  |   dS )zCancel the future if possible.

            Returns True if the future was cancelled, False otherwise. A future
            cannot be cancelled if it is running or has already completed.
            NFT)rM   rU   r   r   r   r   
notify_all_invoke_callbacksr(   r   r   r    cancelW  s   
z_BaseFuture.cancelc                 C   s8   | j  | jttfv W  d   S 1 sw   Y  dS )z(Return True if the future was cancelled.N)rM   rU   r   r   r(   r   r   r    r   j  s   $z_BaseFuture.cancelledc                 C   s4   | j  | jtkW  d   S 1 sw   Y  dS )z1Return True if the future is currently executing.N)rM   rU   r   r(   r   r   r    r   o  s   $z_BaseFuture.runningc                 C   s:   | j  | jtttfv W  d   S 1 sw   Y  dS )zKReturn True of the future was cancelled or finished executing.
            N)rM   rU   r   r   r   r(   r   r   r    rn   t  s
   $z_BaseFuture.donec                 C   s   | j r| j | jS r#   )rs   rr   r(   r   r   r    __get_result{  s   z_BaseFuture.__get_resultc                 C   s`   | j  | jtttfvr| j| 	 W d   dS W d   n1 s%w   Y  ||  dS )aY  Attaches a callable that will be called when the future finishes.

            Args:
                fn: A callable that will be called with this future as its only
                    argument when the future completes or is cancelled. The
                    callable will always be called by a thread in the same
                    process in which it was added. If the future has already
                    completed or been cancelled then the callable will be
                    called immediately. These callables are called in the order
                    that they were added.
            N)rM   rU   r   r   r   rt   r-   )r)   fnr   r   r    add_done_callback  s   z_BaseFuture.add_done_callbackNc                 C   s   | j > | jttfv rt | jtkr|  W  d   S | j | | jttfv r.t | jtkr>|  W  d   S t 1 sDw   Y  dS )a  Return the result of the call that the future represents.

            Args:
                timeout: The number of seconds to wait for the result if the
                    future isn't done. If None, then there is no limit on the
                    wait time.

            Returns:
                The result of the call that the future represents.

            Raises:
                CancelledError: If the future was cancelled.
                TimeoutError: If the future didn't finish executing before the
                    given timeout.
                Exception: If the call raised then that exception will be
                raised.
            N)	rM   rU   r   r   r   r   _BaseFuture__get_resultr   r   r)   rg   r   r   r    result  s   

z_BaseFuture.resultc                 C   s   | j < | jttfv rt | jtkr| jW  d   S | j | | jttfv r-t | jtkr<| jW  d   S t 1 sBw   Y  dS )a  Return the exception raised by the call that the future
            represents.

            Args:
                timeout: The number of seconds to wait for the exception if the
                    future isn't done. If None, then there is no limit on the
                    wait time.

            Returns:
                The exception raised by the call that the future represents or
                None if the call completed without raising.

            Raises:
                CancelledError: If the future was cancelled.
                TimeoutError: If the future didn't finish executing before the
                    given timeout.
            N)	rM   rU   r   r   r   r   rs   r   r   r~   r   r   r    rk     s   

z_BaseFuture.exceptionc                 C   s   | j < | jtkr t| _| jD ]}||  q	 W d   dS | jtkr1t| _	 W d   dS t	dt
| | j td1 sBw   Y  dS )a   Mark the future as running or process any cancel notifications.

            Should only be used by Executor implementations and unit tests.

            If the future has been cancelled (cancel() was called and returned
            True) then any threads waiting on the future completing (though
            calls to as_completed() or wait()) are notified and False is
            returned.

            If the future was not cancelled then it is put in the running state
            (future calls to running() will return True) and True is returned.

            This method should be called by Executor implementations before
            executing the work associated with this future. If this method
            returns False then the work should not be executed.

            Returns:
                False if the Future was cancelled, True otherwise.

            Raises:
                RuntimeError: if this method was already called or if
                    set_result() or set_exception() was called.
            NFTz!Future %s in unexpected state: %szFuture in unexpected state)rM   rU   r   r   r\   r3   r   r   r   criticalrK   RuntimeError)r)   r_   r   r   r    set_running_or_notify_cancel  s"   


z(_BaseFuture.set_running_or_notify_cancelc                 C   ^   | j  || _t| _| jD ]}||  q| j   W d   n1 s$w   Y  |   dS )zSets the return value of work associated with the future.

            Should only be used by Executor implementations and unit tests.
            N)rM   rr   r   rU   r\   r0   rw   rx   )r)   r   r_   r   r   r    
set_result     
z_BaseFuture.set_resultc                 C   r   )zSets the result of the future as being the given exception.

            Should only be used by Executor implementations and unit tests.
            N)rM   rs   r   rU   r\   r2   rw   rx   )r)   rk   r_   r   r   r    set_exception  r   z_BaseFuture.set_exceptionr#   )r   r   r   r   r*   rv   ry   r   r   rn   r}   r|   r   rk   r   r   r   r   r   r   r    rp   7  s    	

!#)rp   c                   @   s:   e Zd ZdZdd Zdd ZdddZd	d
 Zdd ZdS )r   zLThis is an abstract base class for concrete asynchronous executors.
        c                 O   s   t  )a@  Submits a callable to be executed with the given arguments.

            Schedules the callable to be executed as fn(*args, **kwargs) and
            returns a Future instance representing the execution of the
            callable.

            Returns:
                A Future representing the given call.
            )NotImplementedError)r)   r{   rR   kwargsr   r   r    submit  s   
zExecutor.submitc                    sL   | ddurt   fddt| D  fdd}| S )a  Returns an iterator equivalent to map(fn, iter).

            Args:
                fn: A callable that will take as many arguments as there are
                    passed iterables.
                timeout: The maximum number of seconds to wait. If None, then
                    there is no limit on the wait time.
                chunksize: The size of the chunks the iterable will be broken
                    into before being passed to a child process. This argument
                    is only used by ProcessPoolExecutor; it is ignored by
                    ThreadPoolExecutor.

            Returns:
                An iterator equivalent to: map(func, *iterables) but the calls
                may be evaluated out-of-order.

            Raises:
                TimeoutError: If the entire result iterator could not be
                    generated before the given timeout.
                Exception: If fn(*args) raises for any values.
            rg   Nc                    s   g | ]}j  g|R  qS r   )r   )rW   rR   )r{   r)   r   r    
<listcomp>E  s    z Executor.map.<locals>.<listcomp>c               	   3   sb    z%D ]} d u r|   V  q|   t  V  qW D ]} |   qd S D ]} |   q)w r#   )r   rc   ry   )r/   )rh   r]   rg   r   r    result_iteratorI  s   

z%Executor.map.<locals>.result_iterator)getrc   zip)r)   r{   	iterablesr   r   r   )rh   r{   r]   r)   rg   r    map+  s   

zExecutor.mapTc                 C   s   dS )a  Clean-up the resources associated with the Executor.

            It is safe to call this method several times. Otherwise, no other
            methods can be called after this one.

            Args:
                wait: If True then shutdown will not return until all running
                    futures have finished executing and the resources used by
                    the executor have been reclaimed.
            Nr   )r)   r   r   r   r    shutdownU  s   zExecutor.shutdownc                 C   s   | S r#   r   r(   r   r   r    rO   b  s   zExecutor.__enter__c                 C   s   | j dd dS )NT)r   F)r   )r)   exc_typeexc_valexc_tbr   r   r    rS   e  s   zExecutor.__exit__N)T)	r   r   r   r   r   r   r   rO   rS   r   r   r   r    r     s    
*r   c                   @   s   e Zd Zdd ZdS )r	   c              	   C   s:   | j D ]}z||  W q ty   td|  Y qw d S )Nz!exception calling callback for %r)rt   BaseExceptionr   rk   )r)   callbackr   r   r    rx   n  s   
zFuture._invoke_callbacksN)r   r   r   rx   r   r   r   r    r	   m  s    r	   r#   )'sysrc   loggingr$   collectionsversion_infoconcurrent.futuresr   r   r   r   r   r	   rp   r
   r   r   concurrent.futures._baser   r   r   r   r   r   r   _FUTURE_STATESru   	getLogger	Exceptionr   objectr"   r4   r>   rA   rH   ra   
namedtuplerj   r   r   r   r    <module>   sl   	
	
:2 eR