o
    oi                     @   s   d Z ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddgZe	d	j
e d
< eje d< eje d< G dd deZdd ZG dd de
ZG dd deZddlmZ ee d dS )z5
Iterators across greenlets or AsyncResult objects.

    )absolute_import)division)print_function)lock)queueIMapUnorderedIMapgeventGreenlet	SemaphoreUnboundQueuec                   @   s   e Zd ZdZdddZdS )Failureexcraise_exceptionNc                 C   s   || _ || _d S Nr   )selfr   r    r   K/var/www/html/pca-backend/venv/lib/python3.10/site-packages/gevent/_imap.py__init__   s   
zFailure.__init__r   )__name__
__module____qualname__	__slots__r   r   r   r   r   r      s    r   c                 C   s   | j r	|    d S | jr   )r   r   )failurer   r   r   
_raise_exc$   s   r   c                   @   sv   e Zd ZdZdddZdd Zdd	 Ze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 ZdS )r   z%
    At iterator of map results.
    NFc                 C   sV   t |  || _|| _|| _|| _t | _|rt|| _	nd| _	d| _
d| _d| _dS )a  
        An iterator that.

        :param callable spawn: The function we use to create new greenlets.
        :keyword int maxsize: If given and not-None, specifies the maximum number of
            finished results that will be allowed to accumulated awaiting the reader;
            more than that number of results will cause map function greenlets to begin
            to block. This is most useful is there is a great disparity in the speed of
            the mapping code and the consumer and the results consume a great deal of resources.
            Using a bound is more computationally expensive than not using a bound.

        .. versionchanged:: 1.1b3
            Added the *maxsize* parameter.
        Nr   F)r
   r   spawn_zippedfunciterabler   r   r   _result_semaphore_outstanding_tasks
_max_indexfinished)r   r   r    r   maxsizer   r   r   r   r   0   s   

zIMapUnordered.__init__c                 C   s   | S r   r   r   r   r   r   __iter__c   s   zIMapUnordered.__iter__c                 C   s2   | j d ur
| j   |  }t|trt| |S r   )r!   release_inext
isinstancer   r   )r   valuer   r   r   __next__f   s   


zIMapUnordered.__next__c                 C   s
   | j  S r   )r   getr&   r   r   r   r)   p      
zIMapUnordered._inextc                 C   s\   | j d ur
| j   |  jd7  _| js| ||n| j|g|R  }||_|| j |S N   )r!   acquirer"   r   r   _imap_task_indexrawlink
_on_result)r   r   item
item_indexgr   r   r   _ispawns   s   

$zIMapUnordered._ispawnc              
   C   s   zAz| j }| jD ]}|  jd7  _| ||| j q| d  W n ty2 } z| |  d }~ww W d | _d | _ d | _d | _d S d | _d | _ d | _d | _w r/   )r   r    r#   r8   
_on_finishBaseExceptionr   r!   )r   r   r5   er   r   r   _run|   s(   


zIMapUnordered._runc                 C   s   |  j d8  _ | j }| j}|  }d}|r"|dkr"|s"d }| _d}| r0| j| | n	| j| | |rE| j|   d S d S )Nr0   Fr   T)	r"   r$   ready
successfulr   put_iqueue_value_for_success_iqueue_value_for_failure_iqueue_value_for_self_finished)r   greenletcountr$   r=   put_finishedr   r   r   r4      s   
zIMapUnordered._on_resultc                 C   sV   | j rd S |d urd| _ | j| | d S | jdkr)d| _ | j|   d S d S )NTr   )r$   r   r?   _iqueue_value_for_self_failurer"   rB   r   	exceptionr   r   r   r9      s   
zIMapUnordered._on_finishc                 C   s   |j S r   )r+   r   rC   r   r   r   r@      s   z'IMapUnordered._iqueue_value_for_successc                 C   s   t |jt|dS )N_raise_exception)r   rH   getattrrI   r   r   r   rA         z'IMapUnordered._iqueue_value_for_failurec                 C   s
   t t S r   )r   StopIterationr&   r   r   r   rB      r.   z-IMapUnordered._iqueue_value_for_self_finishedc                 C   s   t || jS r   )r   rJ   rG   r   r   r   rF      s   z,IMapUnordered._iqueue_value_for_self_failure)NF)r   r   r   __doc__r   r'   r,   nextr)   r8   r<   r4   r9   r@   rA   rB   rF   r   r   r   r   r   +   s    
3	c                   @   s<   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd ZdS )r   c                 O   s(   i | _ d| _tj| g|R i | d S )Nr   )_resultsindexr   r   )r   argskwargsr   r   r   r      s   zIMap.__init__c                 C   s`   z	| j | j}W n ty&   	 | j \}}|| jkrn|| j |< qY nw |  jd7  _|S r/   )rP   poprQ   KeyErrorr   r-   )r   r+   rQ   r   r   r   r)      s   

zIMap._inextc                 C      |j t| |fS r   )r2   r   r@   rI   r   r   r   r@      rL   zIMap._iqueue_value_for_successc                 C   rV   r   )r2   r   rA   rI   r   r   r   rA      rL   zIMap._iqueue_value_for_failurec                 C   s   | j d t| fS r/   )r#   r   rB   r&   r   r   r   rB      s   z$IMap._iqueue_value_for_self_finishedc                 C   s   | j d t| |fS r/   )r#   r   rF   rG   r   r   r   rF      s   z#IMap._iqueue_value_for_self_failureN)	r   r   r   r   r)   r@   rA   rB   rF   r   r   r   r   r      s    )import_c_accelzgevent.__imapN)rN   
__future__r   r   r   r	   r   r   __all__
__import__r
   localsr   r   objectr   r   r   r   gevent._utilrW   globalsr   r   r   r   <module>   s&    &