B
    ÐH/\  ã               @   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)Ú
_semaphore)Ú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 )N)r   r   )Úselfr   r   © r   ú+lib/python3.7/site-packages/gevent/_imap.pyÚ__init__   s    zFailure.__init__)N)Ú__name__Ú
__module__Ú__qualname__Ú	__slots__r   r   r   r   r   r      s   r   c             C   s   | j r|   ¡  n| j‚d S )N)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 )Nr   )r   r   r   r   Ú__iter__c   s    zIMapUnordered.__iter__c             C   s2   | j d k	r| j  ¡  |  ¡ }t|tƒr.t|ƒ |S )N)r   ÚreleaseÚ_inextÚ
isinstancer   r   )r   Úvaluer   r   r   Ú__next__f   s    


zIMapUnordered.__next__c             C   s
   | j  ¡ S )N)r   Úget)r   r   r   r   r%   p   s    zIMapUnordered._inextc             C   sZ   | j d k	r| j  ¡  |  jd7  _| js4|  ||¡n| j|f|žŽ }||_| | 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–   zvyB| j }x,| jD ]"}|  jd7  _|  ||| j¡ qW |  d ¡ W n. tk
rr } z|  |¡ ‚ W d d }~X Y nX W d d | _d | _ d | _d | _X d S )Nr*   )r   r   r    r0   Ú
_on_finishÚBaseExceptionr   r   )r   r   r.   Úer   r   r   Ú_run|   s    
zIMapUnordered._runc             C   sŠ   |  j d8  _ | j }| j}|  ¡ }d}|rD|dkrD|sDd }| _d}| ¡ r`| j |  |¡¡ n| j |  |¡¡ |r†| j |  ¡ ¡ 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!   r5   Zput_finishedr   r   r   r-   Œ   s    
zIMapUnordered._on_resultc             C   sR   | j r
d S |d k	r.d| _ | j |  |¡¡ d S | jdkrNd| _ | j |  ¡ ¡ d S )NTr   )r!   r   r6   Ú_iqueue_value_for_self_failurer   r9   )r   Ú	exceptionr   r   r   r1       s    
zIMapUnordered._on_finishc             C   s   |j S )N)r'   )r   r:   r   r   r   r7   ®   s    z'IMapUnordered._iqueue_value_for_successc             C   s   t |jt|dƒƒS )NÚ_raise_exception)r   r=   Úgetattr)r   r:   r   r   r   r8   ±   s    z'IMapUnordered._iqueue_value_for_failurec             C   s
   t tƒ ƒS )N)r   ÚStopIteration)r   r   r   r   r9   ´   s    z-IMapUnordered._iqueue_value_for_self_finishedc             C   s   t || jƒS )N)r   r>   )r   r=   r   r   r   r<   ·   s    z,IMapUnordered._iqueue_value_for_self_failure)NF)r   r   r   Ú__doc__r   r#   r(   Únextr%   r0   r4   r-   r1   r7   r8   r9   r<   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| f|ž|Ž d S )Nr   )Ú_resultsÚindexr   r   )r   ÚargsÚkwargsr   r   r   r   À   s    zIMap.__init__c             C   sf   y| j  | j¡}W n@ tk
rR   x*| j ¡ \}}|| jkr@P q$|| j |< q$W Y nX |  jd7  _|S )Nr*   )rC   ÚpoprD   ÚKeyErrorr   r)   )r   r'   rD   r   r   r   r%   È   s    
zIMap._inextc             C   s   |j t | |¡fS )N)r,   r   r7   )r   r:   r   r   r   r7   Ö   s    zIMap._iqueue_value_for_successc             C   s   |j t | |¡fS )N)r,   r   r8   )r   r:   r   r   r   r8   Ù   s    zIMap._iqueue_value_for_failurec             C   s   | j d t | ¡fS )Nr*   )r    r   r9   )r   r   r   r   r9   Ü   s    z$IMap._iqueue_value_for_self_finishedc             C   s   | j d t | |¡fS )Nr*   )r    r   r<   )r   r=   r   r   r   r<   ß   s    z#IMap._iqueue_value_for_self_failureN)	r   r   r   r   r%   r7   r8   r9   r<   r   r   r   r   r   »   s   )Úimport_c_accelzgevent.__imapN)rA   Z
__future__r   r   r   r	   r   r   Ú__all__Ú
__import__r
   Úlocalsr   r   Úobjectr   r   r   r   Zgevent._utilrI   Úglobalsr   r   r   r   Ú<module>   s"    '