U
    @fG                     @   s   d Z ddlZddlZddlZddlZddlmZ ddlmZ ddl	m
Z
 ddlZddlmZ ddd	d
ddgZe ZejZejZG dd dZG dd
 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dS )a  Synchronized queues.

The :mod:`eventlet.queue` module implements multi-producer, multi-consumer
queues that work across greenlets, with the API similar to the classes found in
the standard :mod:`Queue` and :class:`multiprocessing <multiprocessing.Queue>`
modules.

A major difference is that queues in this module operate as channels when
initialized with *maxsize* of zero. In such case, both :meth:`Queue.empty`
and :meth:`Queue.full` return ``True`` and :meth:`Queue.put` always blocks until
a call to :meth:`Queue.get` retrieves the item.

An interesting difference, made possible because of greenthreads, is
that :meth:`Queue.qsize`, :meth:`Queue.empty`, and :meth:`Queue.full` *can* be
used as indicators of whether the subsequent :meth:`Queue.get`
or :meth:`Queue.put` will not block.  The new methods :meth:`Queue.getting`
and :meth:`Queue.putting` report on the number of greenthreads blocking
in :meth:`put <Queue.put>` or :meth:`get <Queue.get>` respectively.
    N)Event)
getcurrent)get_hub)TimeoutQueuePriorityQueue	LifoQueue
LightQueueFullEmptyc                   @   s`   e Zd ZdZdgZdd Zdd Zdd Zd	d
 ZeZ	e
dd ZdddZdd Zdd ZdS )Waitera6  A low level synchronization class.

    Wrapper around greenlet's ``switch()`` and ``throw()`` calls that makes them safe:

    * switching will occur only if the waiting greenlet is executing :meth:`wait`
      method currently. Otherwise, :meth:`switch` and :meth:`throw` are no-ops.
    * any error raised in the greenlet is handled inside :meth:`switch` and :meth:`throw`

    The :meth:`switch` and :meth:`throw` methods must only be called from the :class:`Hub` greenlet.
    The :meth:`wait` method must be called from a greenlet other than :class:`Hub`.
    greenletc                 C   s
   d | _ d S Nr   self r   H/var/www/html/chatgpt/venv/lib/python3.8/site-packages/eventlet/queue.py__init__L   s    zWaiter.__init__c                 C   s0   | j rd}nd}dt| jtt| || jf S )N waiting z<%s at %s%s greenlet=%r>)waitingtype__name__hexidr   r   r   r   r   r   __repr__O   s     
  zWaiter.__repr__c                 C   s&   | j rd}nd}dt| j|| jf S )zD
        >>> print(Waiter())
        <Waiter greenlet=None>
        r   r   z<%s%s greenlet=%s>)r   r   r   r   r   r   r   r   __str__X   s    zWaiter.__str__c                 C   s
   | j d k	S r   r   r   r   r   r   __nonzero__c   s    zWaiter.__nonzero__c                 C   s
   | j d k	S r   r   r   r   r   r   r   h   s    zWaiter.waitingNc                 C   sR   t  t jkstd| jdk	rNz| j| W n tk
rL   t  Y nX dS )zWake up the greenlet that is calling wait() currently (if there is one).
        Can only be called from Hub's greenlet.
        3Can only use Waiter.switch method from the mainloopN)r   r   r   AssertionErrorswitch	Exception	traceback	print_exc)r   valuer   r   r   r"   l   s    
zWaiter.switchc                 G   sR   t  t jkstd| jdk	rNz| jj|  W n tk
rL   t  Y nX dS )zuMake greenlet calling wait() wake up (if there is a wait()).
        Can only be called from Hub's greenlet.
        r    N)r   r   r   r!   throwr#   r$   r%   )r   Z
throw_argsr   r   r   r'   x   s    
zWaiter.throwc                 C   s>   | j dkstd| j f t | _ zt  W S d| _ X dS )z2Wait until switch() or throw() is called.
        Nz!This Waiter is already used by %r)r   r!   r   r   r"   r   r   r   r   wait   s
    zWaiter.wait)N)r   
__module____qualname____doc__	__slots__r   r   r   r   __bool__propertyr   r"   r'   r(   r   r   r   r   r   >   s   	

r   c                   @   s   e Zd ZdZd*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d Zdd Zdd Zdd Zd+ddZd d! Zd,d"d#Zd$d% Zd&d' Zd(d) ZdS )-r	   a   
    This is a variant of Queue that behaves mostly like the standard
    :class:`Stdlib_Queue`.  It differs by not supporting the
    :meth:`task_done <Stdlib_Queue.task_done>` or
    :meth:`join <Stdlib_Queue.join>` methods, and is a little faster for
    not having that overhead.
    Nc                 C   sB   |d ks|dk rd | _ n|| _ t | _t | _d | _| | d S Nr   )maxsizesetgettersputters_event_unlock_initr   r0   r   r   r   r      s    zLightQueue.__init__c                 C   s   t  | _d S r   )collectionsdequequeuer6   r   r   r   r5      s    zLightQueue._initc                 C   s
   | j  S r   )r9   popleftr   r   r   r   _get   s    zLightQueue._getc                 C   s   | j | d S r   )r9   appendr   itemr   r   r   _put   s    zLightQueue._putc                 C   s    dt | jtt| |  f S )Nz<%s at %s %s>)r   r   r   r   _formatr   r   r   r   r      s    zLightQueue.__repr__c                 C   s   dt | j|  f S )Nz<%s %s>)r   r   r@   r   r   r   r   r      s    zLightQueue.__str__c                 C   sl   d| j f }t| dd r&|d| j 7 }| jr>|dt| j 7 }| jrV|dt| j 7 }| jd k	rh|d7 }|S )Nz
maxsize=%rr9   z	 queue=%rz getters[%s]z putters[%s]z
 unlocking)r0   getattrr9   r2   lenr3   r4   r   resultr   r   r   r@      s    
zLightQueue._formatc                 C   s
   t | jS )zReturn the size of the queue.)rB   r9   r   r   r   r   qsize   s    zLightQueue.qsizec                 C   s.   | j dk	r$|dks|| j kr$|   || _ dS )zyResizes the queue's maximum size.

        If the size is increased, and there are putters waiting, they may be woken up.N)r0   _schedule_unlock)r   sizer   r   r   resize   s    zLightQueue.resizec                 C   s
   t | jS )z`Returns the number of greenthreads that are blocked waiting to put
        items into the queue.)rB   r3   r   r   r   r   putting   s    zLightQueue.puttingc                 C   s
   t | jS )zVReturns the number of greenthreads that are blocked waiting on an
        empty queue.)rB   r2   r   r   r   r   getting   s    zLightQueue.gettingc                 C   s
   |    S )z;Return ``True`` if the queue is empty, ``False`` otherwise.)rE   r   r   r   r   empty   s    zLightQueue.emptyc                 C   s   | j dk	o|  | j kS )zkReturn ``True`` if the queue is full, ``False`` otherwise.

        ``Queue(None)`` is never full.
        N)r0   rE   r   r   r   r   full   s    zLightQueue.fullTc              	   C   sf  | j dks|  | j k r4| | | jr0|   n.|st jt kr| jr|| j }|rF| | | 	 }|
| dS qFtn|rt||}| j| t|t}zD| jr|   | }||kstd|f |jtk	r| | W 5 |  | j| X n\| jr^t||}| j| |   | }||ksLtd|f |jtk	rbtntdS )a#  Put an item into the queue.

        If optional arg *block* is true and *timeout* is ``None`` (the default),
        block if necessary until a free slot is available. If *timeout* is
        a positive number, it blocks at most *timeout* seconds and raises
        the :class:`Full` exception if no free slot was available within that time.
        Otherwise (*block* is false), put an item on the queue if a free slot
        is immediately available, else raise the :class:`Full` exception (*timeout*
        is ignored in that case).
        Nz!Invalid switch into Queue.put: %r)r0   rE   r?   r2   rF   r   r   r   popr;   r"   r
   
ItemWaiterr3   addr   canceldiscardr(   r!   r>   _NONE)r   r>   blocktimeoutgetterwaiterrD   r   r   r   put   sF    







zLightQueue.putc                 C   s   |  |d dS )zPut an item into the queue without blocking.

        Only enqueue the item if a free slot is immediately available.
        Otherwise raise the :class:`Full` exception.
        FN)rW   r=   r   r   r   
put_nowait  s    zLightQueue.put_nowaitc              	   C   s   |   r| jr|   |  S |sft jt krf| jr`| j }|r0|| |   r0|  S q0t	nv|rt
 }t|t	}zD| j| | jr|   z| W W S    |    Y nX W 5 | j| |  X nt	dS )a  Remove and return an item from the queue.

        If optional args *block* is true and *timeout* is ``None`` (the default),
        block if necessary until an item is available. If *timeout* is a positive number,
        it blocks at most *timeout* seconds and raises the :class:`Empty` exception
        if no item was available within that time. Otherwise (*block* is false), return
        an item if one is immediately available, else raise the :class:`Empty` exception
        (*timeout* is ignored in that case).
        N)rE   r3   rF   r;   r   r   r   rM   r"   r   r   r   r2   rQ   rP   rO   r(   )r   rS   rT   putterrV   r   r   r   get   s6    




zLightQueue.getc                 C   s
   |  dS )zRemove and return an item from the queue without blocking.

        Only get an item if one is immediately available. Otherwise
        raise the :class:`Empty` exception.
        F)rZ   r   r   r   r   
get_nowaitJ  s    zLightQueue.get_nowaitc                 C   sf  zV|  rT| jrT| j }|rRz|  }W n   |jt   Y nX || q| j	r| jr| j	 }|r| j }|r|j
}t|_
| | |  }|| || n| j	| q| j	r| js| jd ks|  | jk r| j	 }|| q| j	rV| jsVdd | j	D }|s"qV|D ]&}| j	| t d|jjt q&qqVqW 5 d | _ X d S )Nc                 S   s   g | ]}|j s|qS r   )rS   ).0pr   r   r   
<listcomp>r  s      z&LightQueue._unlock.<locals>.<listcomp>r   )r4   rE   r2   rM   r;   r'   sysexc_infor"   r3   r>   rR   r?   rO   r0   rQ   r   schedule_call_globalr   r
   )r   rU   r>   rY   rL   r   r   r   _unlockR  sR    





  

zLightQueue._unlockc                 C   s    | j d krt d| j| _ d S r/   )r4   r   ra   rb   r   r   r   r   rF     s    
zLightQueue._schedule_unlock)N)TN)TN)r   r)   r*   r+   r   r5   r;   r?   r   r   r@   rE   rH   rI   rJ   rK   rL   rW   rX   rZ   r[   rb   rF   r   r   r   r   r	      s(   


4
*/c                   @   s   e Zd ZddgZdd ZdS )rN   r>   rS   c                 C   s   t |  || _|| _d S r   )r   r   r>   rS   )r   r>   rS   r   r   r   r     s    
zItemWaiter.__init__N)r   r)   r*   r,   r   r   r   r   r   rN     s   rN   c                   @   sB   e Zd ZdZdddZdd Zdd Zd	d
 Zdd Zdd Z	dS )r   a  Create a queue object with a given maximum size.

    If *maxsize* is less than zero or ``None``, the queue size is infinite.

    ``Queue(0)`` is a channel, that is, its :meth:`put` method always blocks
    until the item is delivered. (This is unlike the standard
    :class:`Stdlib_Queue`, where 0 means infinite size).

    In all other respects, this Queue class resembles the standard library,
    :class:`Stdlib_Queue`.
    Nc                 C   s   t | | d| _t | _d S r/   )r	   r   unfinished_tasksr   _condr6   r   r   r   r     s    zQueue.__init__c                 C   s(   t | }| jr$|d| j| jf 7 }|S )Nz tasks=%s _cond=%s)r	   r@   rc   rd   rC   r   r   r   r@     s    
zQueue._formatc                 C   s   t | | |   d S r   )r	   r?   _put_bookkeepingr=   r   r   r   r?     s    z
Queue._putc                 C   s&   |  j d7  _ | j r"| j  d S )N   )rc   rd   readyresetr   r   r   r   re     s    
zQueue._put_bookkeepingc                 C   s:   | j dkrtd|  j d8  _ | j dkr6| jd dS )aY  Indicate that a formerly enqueued task is complete. Used by queue consumer threads.
        For each :meth:`get <Queue.get>` used to fetch a task, a subsequent call to
        :meth:`task_done` tells the queue that the processing on the task is complete.

        If a :meth:`join` is currently blocking, it will resume when all items have been processed
        (meaning that a :meth:`task_done` call was received for every item that had been
        :meth:`put <Queue.put>` into the queue).

        Raises a :exc:`ValueError` if called more times than there were items placed in the queue.
        r   z!task_done() called too many timesrf   N)rc   
ValueErrorrd   sendr   r   r   r   	task_done  s
    

zQueue.task_donec                 C   s   | j dkr| j  dS )a  Block until all items in the queue have been gotten and processed.

        The count of unfinished tasks goes up whenever an item is added to the queue.
        The count goes down whenever a consumer thread calls :meth:`task_done` to indicate
        that the item was retrieved and all work on it is complete. When the count of
        unfinished tasks drops to zero, :meth:`join` unblocks.
        r   N)rc   rd   r(   r   r   r   r   join  s    
z
Queue.join)N)
r   r)   r*   r+   r   r@   r?   re   rk   rl   r   r   r   r   r     s   
c                   @   s4   e Zd ZdZdd ZejfddZejfddZ	dS )	r   zA subclass of :class:`Queue` that retrieves entries in priority order (lowest first).

    Entries are typically tuples of the form: ``(priority number, data)``.
    c                 C   s
   g | _ d S r   r9   r6   r   r   r   r5     s    zPriorityQueue._initc                 C   s   || j | |   d S r   )r9   re   )r   r>   heappushr   r   r   r?     s    zPriorityQueue._putc                 C   s
   || j S r   rm   )r   heappopr   r   r   r;     s    zPriorityQueue._getN)
r   r)   r*   r+   r5   heapqrn   r?   ro   r;   r   r   r   r   r     s   c                   @   s(   e Zd ZdZdd Zdd Zdd ZdS )	r   zNA subclass of :class:`Queue` that retrieves most recently added entries first.c                 C   s
   g | _ d S r   rm   r6   r   r   r   r5     s    zLifoQueue._initc                 C   s   | j | |   d S r   )r9   r<   re   r=   r   r   r   r?     s    zLifoQueue._putc                 C   s
   | j  S r   )r9   rM   r   r   r   r   r;     s    zLifoQueue._getN)r   r)   r*   r+   r5   r?   r;   r   r   r   r   r     s   )r+   r_   rp   r7   r$   Zeventlet.eventr   Zeventlet.greenthreadr   Zeventlet.hubsr   r9   ZStdlib_QueueZeventlet.timeoutr   __all__objectrR   r
   r   r   r	   rN   r   r   r   r   r   r   r   <module>   s(   R w	?