o
    ‡eø  ã                   @   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   ú<D:\Projects\ConvertPro\env\Lib\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   )Z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_indexZrawlinkÚ
_on_result)r   r   ÚitemZ
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"   r5   Ú
_on_finishÚBaseExceptionr   r    )r   r   r3   Ú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 )Nr/   Fr   T)	r!   r#   ÚreadyZ
successfulr   ÚputÚ_iqueue_value_for_successÚ_iqueue_value_for_failureÚ_iqueue_value_for_self_finished)r   ÚgreenletÚcountr#   r:   Zput_finishedr   r   r   r2   Œ   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!   r>   ©r   Ú	exceptionr   r   r   r6       s   
þzIMapUnordered._on_finishc                 C   s   |j S r   )r*   ©r   r?   r   r   r   r<   ®   s   z'IMapUnordered._iqueue_value_for_successc                 C   s   t |jt|dƒƒS )NÚ_raise_exception)r   rC   ÚgetattrrD   r   r   r   r=   ±   ó   z'IMapUnordered._iqueue_value_for_failurec                 C   s
   t tƒ ƒS r   )r   ÚStopIterationr%   r   r   r   r>   ´   r-   z-IMapUnordered._iqueue_value_for_self_finishedc                 C   s   t || jƒS r   )r   rE   rB   r   r   r   rA   ·   s   z,IMapUnordered._iqueue_value_for_self_failure)NF)r   r   r   Ú__doc__r   r&   r+   Únextr(   r5   r9   r2   r6   r<   r=   r>   rA   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.   )rK   ÚpoprL   ÚKeyErrorr   r,   )r   r*   rL   r   r   r   r(   È   s   

üûzIMap._inextc                 C   ó   |j t | |¡fS r   )r1   r   r<   rD   r   r   r   r<   Õ   rG   zIMap._iqueue_value_for_successc                 C   rQ   r   )r1   r   r=   rD   r   r   r   r=   Ø   rG   zIMap._iqueue_value_for_failurec                 C   s   | j d t | ¡fS r.   )r"   r   r>   r%   r   r   r   r>   Û   s   z$IMap._iqueue_value_for_self_finishedc                 C   s   | j d t | |¡fS r.   )r"   r   rA   rB   r   r   r   rA   Þ   s   z#IMap._iqueue_value_for_self_failureN)	r   r   r   r   r(   r<   r=   r>   rA   r   r   r   r   r   »   s    )Úimport_c_accelzgevent.__imapN)rI   Ú
__future__r   r   r   r	   r   r   Ú__all__Ú
__import__r
   Úlocalsr   r   Úobjectr   r   r   r   Zgevent._utilrR   Úglobalsr   r   r   r   Ú<module>   s&   þ &