a
    pw¦Z¥  ã                   @   s    d dl mZ G dd„ deƒZdS )é    )Údequec                   @   s:   e Zd ZdZddd„Zdd„ Zdd„ Zd	d
„ Zdd„ ZdS )ÚRoundRobinQueuea1  A round robin queue implemented using multiple internal queues (typically,
    FIFO queues). The internal queue must implement the following methods:
        * push(obj)
        * pop()
        * close()
        * __len__()
    The constructor receives a qfactory argument, which is a callable used to
    instantiate a new (internal) queue when a new key is allocated. The
    qfactory function is called with the key number as first and only
    argument.
    start_keys is a sequence of domains to start with. If the queue was
    previously closed leaving some domain buckets non-empty, those domains
    should be passed in start_keys.

    The queue maintains a fifo queue of keys.  The key that went last is
    poped first and the next queue for that key is then poped.  This allows
    for a round robin
    © c                 C   s4   i | _ || _|D ]}|  |¡| j |< qt|ƒ| _d S ©N)ÚqueuesÚqfactoryr   Ú	key_queue)Úselfr   Zstart_domainsÚkeyr   r   ú/lib/python3.9/site-packages/queuelib/rrqueue.pyÚ__init__   s
    zRoundRobinQueue.__init__c                 C   s>   || j vr&|  |¡| j|< | j  |¡ | j| }| |¡ d S r   )r   r   r   Ú
appendleftÚpush)r	   Úobjr
   Úqr   r   r   r      s
    

zRoundRobinQueue.pushc                 C   sz   d }|d u rvz| j  ¡ }W n ty.   Y d S 0 | j| }| ¡ }t|ƒdkr`| j|= | ¡  n| j  |¡ |r|S qd S )Nr   )r   ÚpopÚ
IndexErrorr   ÚlenÚcloser   )r	   Úmr
   r   r   r   r   r   '   s    

zRoundRobinQueue.popc                 C   s6   g }| j  ¡ D ]"\}}t|ƒr(| |¡ | ¡  q|S r   )r   Úitemsr   Úappendr   )r	   ZactiveÚkr   r   r   r   r   <   s    

zRoundRobinQueue.closec                 C   s"   | j rtdd„ | j  ¡ D ƒƒS dS )Nc                 s   s   | ]}t |ƒV  qd S r   )r   )Ú.0Úxr   r   r   Ú	<genexpr>E   ó    z*RoundRobinQueue.__len__.<locals>.<genexpr>r   )r   ÚsumÚvalues)r	   r   r   r   Ú__len__D   s    zRoundRobinQueue.__len__N)r   )	Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r   r   r   r   r   r      s   
r   N)Úcollectionsr   Úobjectr   r   r   r   r   Ú<module>   s   