o
    0׾gQ[                     @   s  d Z ddlmZ ddlZddlmZ ddlmZ ddlm	Z
 ddlZejd dkr.ddlZnddlZej ZZej ZZddlmZ dd	lmZ dd
lmZ g Zg dZddgZddgZeedrie d ej!Z"eee e 7 Zdd Z#ddl$Z%e%j&j'e( d< e)dj*e( d< dd e( d< G dd de'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/G d#d deZ0G d$d de,Z1d%d& Z2e2  dd'l3m4Z4 e4e5 d( dS ))al  
Synchronized queues.

The :mod:`gevent.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.

The classes in this module implement the iterator protocol. Iterating
over a queue means repeatedly calling :meth:`get <Queue.get>` until
:meth:`get <Queue.get>` returns ``StopIteration`` (specifically that
class, not an instance or subclass).

    >>> import gevent.queue
    >>> queue = gevent.queue.Queue()
    >>> queue.put(1)
    >>> queue.put(2)
    >>> queue.put(StopIteration)
    >>> for item in queue:
    ...    print(item)
    1
    2

.. versionchanged:: 1.0
       ``Queue(0)`` now means queue of infinite size, not a channel. A :exc:`DeprecationWarning`
       will be issued with this argument.
    )absolute_importN)heappush)heappop)heapify   )Timeout)get_hub_noargs)InvalidSwitchError)QueuePriorityQueue	LifoQueueJoinableQueueChannelEmptyFullSimpleQueuec                 C   s&   z|  | W d S  ty   Y d S w N)remove
ValueError)deqitem r   V/var/www/html/backend_erp/backend_erp_env/lib/python3.10/site-packages/gevent/queue.py_safe_removeC   s
   r   Waitergreenlet
getcurrentc                   C   s   d S r   r   r   r   r   r   <lambda>N   s    r   greenlet_initc                   @   s    e Zd ZdZdd Zdd ZdS )
ItemWaiter)r   queuec                 C   s   t |  || _|| _d S r   )r   __init__r   r    )selfr   r    r   r   r   r!   W   s   

zItemWaiter.__init__c                 C   s$   | j | j d | _ d | _| | S r   )r    _putr   switchr"   r   r   r   put_and_switch\   s   
zItemWaiter.put_and_switchN)__name__
__module____qualname__	__slots__r!   r&   r   r   r   r   r   P   s    r   c                   @   s  e Zd ZdZdZd>ddZedd	 Zejd
d	 Zd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- Zd@d.d/Zd0d1 Zd@d2d3Zd4d5 Zd6d7 Zd8d9 Zd:d; Z d<d= Z!e!Z"dS )Ar
   a  
    Create a queue object with a given maximum size.

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

    Queues have a ``len`` equal to the number of items in them (the :meth:`qsize`),
    but in a boolean context they are always True.

    .. versionchanged:: 1.1b3
       Queues now support :func:`len`; it behaves the same as :meth:`qsize`.
    .. versionchanged:: 1.1b3
       Multiple greenlets that block on a call to :meth:`put` for a full queue
       will now be awakened to put their items into the queue in the order in which
       they arrived. Likewise, multiple greenlets that block on a call to :meth:`get` for
       an empty queue will now receive items in the order in which they blocked. An
       implementation quirk under CPython *usually* ensured this was roughly the case
       previously anyway, but that wasn't the case for PyPy.
    )_maxsizegettersputtershub_event_unlockr    __weakref__Nr   r   c                 C   sx   |d ur|dkr|dkrdd l }|jdt|d d }|d ur |nd| _t | _t | _t | _	d | _
| || _d S )Nr   zJQueue(0) now equivalent to Queue(None); if you want a channel, use Channel)
stacklevel)warningswarnDeprecationWarningr+   collectionsdequer,   r-   get_hubr.   r/   _create_queuer    )r"   maxsizeitems_warn_depthr3   r   r   r   r!      s   

zQueue.__init__c                 C   s   | j dkr| j S d S Nr   r+   r%   r   r   r   r:         zQueue.maxsizec                 C   s$   |d u s|dkrd| _ d S || _ d S )Nr   r2   r>   )r"   nvr   r   r   r:      s   

c                 C   s   t | | j| jS r   )typer:   r    r%   r   r   r   copy   s   z
Queue.copyc                 C   s
   t |S r   )r6   r7   r"   r;   r   r   r   r9         
zQueue._create_queuec                 C   
   | j  S r   )r    popleftr%   r   r   r   _get   rD   z
Queue._getc                 C   
   | j d S r=   r    r%   r   r   r   _peek   rD   zQueue._peekc                 C      | j | d S r   r    appendr"   r   r   r   r   r#         z
Queue._putc                 C       dt | jtt| |  f S )Nz<%s at %s%s>rA   r'   hexid_formatr%   r   r   r   __repr__       zQueue.__repr__c                 C      dt | j|  f S )Nz<%s%s>rA   r'   rT   r%   r   r   r   __str__      zQueue.__str__c                 C   s   g }| j d ur|d| j f  t| dd r|d| jf  | jr,|dt| j  | jr9|dt| j  |rBdd| S dS )Nz
maxsize=%rr    zqueue=%rzgetters[%s]zputters[%s]  )r:   rM   getattrr    r,   lenr-   joinr"   resultr   r   r   rT      s   
zQueue._formatc                 C   
   t | jS )zReturn the size of the queue.)r^   r    r%   r   r   r   qsize      
zQueue.qsizec                 C   s   |   S )z
        Return the size of the queue. This is the same as :meth:`qsize`.

        .. versionadded: 1.1b3

            Previously, getting len() of a queue would raise a TypeError.
        rc   r%   r   r   r   __len__   s   	zQueue.__len__c                 C      dS )z
        A queue object is always True.

        .. versionadded: 1.1b3

           Now that queues support len(), they need to implement ``__bool__``
           to return True for backwards compatibility.
        Tr   r%   r   r   r   __bool__   s   	zQueue.__bool__c                 C   rg   NTr   r%   r   r   r   __nonzero__   s   zQueue.__nonzero__c                 C   s
   |    S )z;Return ``True`` if the queue is empty, ``False`` otherwise.re   r%   r   r   r   empty   rd   zQueue.emptyc                 C   s   | j dko|  | j kS )zkReturn ``True`` if the queue is full, ``False`` otherwise.

        ``Queue(None)`` is never full.
        r   )r+   rc   r%   r   r   r   full   s   z
Queue.fullTc              	   C   s8  | j dks|  | j k r| | | jr|   dS dS | jt u rX| jrH|  rH|  | j krH| j }|| | jrH|  rH|  | j ks0|  | j k rV| | dS t	|rt
|| }| j| t|t	}z#| jrs|   | }||urtd|f W |  t| j| dS |  t| j| w t	)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).
        r2   Nz!Invalid switch into Queue.put: %r)r+   rc   r#   r,   _schedule_unlockr.   r   rF   r$   r   r   r-   rM   r   _start_new_or_dummygetr	   cancelr   r"   r   blocktimeoutgetterwaiterra   r   r   r   put   s<   




z	Queue.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rv   rN   r   r   r   
put_nowait"  s   zQueue.put_nowaitc              	   C   s   | j t u r| jr| j   |  r| S | js	t|s tt }t	|t}z*| j
| | jr7|   | }||urFtd|f | W |  t| j
| S |  t| j
| w )Nz!Invalid switch into Queue.get: %r)r.   r   r-   rF   r&   rc   r   r   r   rn   r,   rM   rm   ro   r	   rp   r   )r"   methodrr   rs   ru   ra   r   r   r   __get_or_peek+  s.   zQueue.__get_or_peekc                 C   s.   |   r| jr|   |  S | | j||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).
        )rc   r-   rm   rG   _Queue__get_or_peekr"   rr   rs   r   r   r   ro   O  s
   
z	Queue.getc                 C   
   |  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ro   r%   r   r   r   
get_nowait`     
zQueue.get_nowaitc                 C   s    |   r|  S | | j||S )a  Return an item from the queue without removing it.

        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).
        )rc   rJ   r{   r|   r   r   r   peekh  s   
z
Queue.peekc                 C   r}   )zReturn an item from the queue without blocking.

        Only return an item if one is immediately available. Otherwise
        raise the :class:`Empty` exception.
        F)r   r%   r   r   r   peek_nowaity  r   zQueue.peek_nowaitc                 C   s   	 d}| j r3| jdks|  | jk r3d}z| j  }| |j W n   |jt   Y n|	| | j
rF|  rFd}| j
 }|	| |sJd S q)NTFr2   )r-   r+   rc   rF   r#   r   throwsysexc_infor$   r,   )r"   repeatputterrt   r   r   r   _unlock  s"   



zQueue._unlockc                 C       | j s| jj| j| _ d S d S r   r/   r.   looprun_callbackr   r%   r   r   r   rm        zQueue._schedule_unlockc                 C      | S r   r   r%   r   r   r   __iter__     zQueue.__iter__c                 C      |   }|tu r
||S r   ro   StopIterationr`   r   r   r   __next__     zQueue.__next__)Nr   r   r   TN)#r'   r(   r)   __doc__r*   r!   propertyr:   setterrB   r9   rG   rJ   r#   rU   rY   rT   rc   rf   rh   rj   rk   rl   rv   rx   r{   ro   r   r   r   r   rm   r   r   nextr   r   r   r   r
   b   sD    





*	
$
r
   c                   @   s$   e Zd ZdZdddZd	ddZdS )
UnboundQueuer   Nc                 C   s(   |d urt dt| || d | _d S )NzUnboundQueue has no maxsize)r   r
   r!   r-   )r"   r:   r;   r   r   r   r!     s   
zUnboundQueue.__init__Tc                 C   s    |  | | jr|   d S d S r   )r#   r,   rm   )r"   r   rr   rs   r   r   r   rv     s   
zUnboundQueue.put)Nr   r   )r'   r(   r)   r*   r!   rv   r   r   r   r   r     s    
r   c                   @   s.   e Zd ZdZdZd
ddZdd Zdd Zd	S )r   a  A subclass of :class:`Queue` that retrieves entries in priority order (lowest first).

    Entries are typically tuples of the form: ``(priority number, data)``.

    .. versionchanged:: 1.2a1
       Any *items* given to the constructor will now be passed through
       :func:`heapq.heapify` to ensure the invariants of this class hold.
       Previously it was just assumed that they were already a heap.
    r   c                 C   s   t |}t| |S r   )list_heapify)r"   r;   qr   r   r   r9     s   zPriorityQueue._create_queuec                 C   s   t | j| d S r   )	_heappushr    rN   r   r   r   r#     rO   zPriorityQueue._putc                 C   rb   r   )_heappopr    r%   r   r   r   rG     rD   zPriorityQueue._getNr   )r'   r(   r)   r   r*   r9   r#   rG   r   r   r   r   r     s    

r   c                   @   s6   e Zd ZdZdZdd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.r   c                 C   s   t |S r   )r   rC   r   r   r   r9     s   zLifoQueue._create_queuec                 C   rK   r   rL   rN   r   r   r   r#     rO   zLifoQueue._putc                 C   rE   r   )r    popr%   r   r   r   rG     rD   zLifoQueue._getc                 C   rH   )Nr2   rI   r%   r   r   r   rJ     rD   zLifoQueue._peekNr   )	r'   r(   r)   r   r*   r9   r#   rG   rJ   r   r   r   r   r     s    
r   c                   @   sH   e Zd ZdZdZdddZdd Zd	d
 Zdd Zdd Z	dddZ
dS )r   zl
    A subclass of :class:`Queue` that additionally has
    :meth:`task_done` and :meth:`join` methods.
    )_condunfinished_tasksNr   c                 C   sj   t j| ||dd ddlm} | | _| j  |r|| _n|r&t|| _nd| _| jr3| j  dS dS )z

        .. versionchanged:: 1.1a1
           If *unfinished_tasks* is not given, then all the given *items*
           (if any) will be considered unfinished.

           )r<   r   )EventN)	r
   r!   gevent.eventr   r   setr   r^   clear)r"   r:   r;   r   r   r   r   r   r!     s   
zJoinableQueue.__init__c                 C   s   t | | j| j| jS r   )rA   r:   r    r   r%   r   r   r   rB     rZ   zJoinableQueue.copyc                 C   s(   t | }| jr|d| j| jf 7 }|S )Nz tasks=%s _cond=%s)r
   rT   r   r   r`   r   r   r   rT     s   
zJoinableQueue._formatc                 C   s(   t | | |  jd7  _| j  d S )N   )r
   r#   r   r   r   rN   r   r   r   r#     s   zJoinableQueue._putc                 C   s<   | j dkr	td|  j d8  _ | j dkr| j  dS 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 timesr   N)r   r   r   r   r%   r   r   r   	task_done  s   

zJoinableQueue.task_donec                 C   s   | j 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.

        :param float timeout: If not ``None``, then wait no more than this time in seconds
            for all tasks to finish.
        :return: ``True`` if all tasks have finished; if ``timeout`` was given and expired before
            all tasks finished, ``False``.

        .. versionchanged:: 1.1a1
           Add the *timeout* parameter.
        )rs   )r   wait)r"   rs   r   r   r   r_   #  s   zJoinableQueue.join)Nr   Nr   )r'   r(   r)   r   r*   r!   rB   rT   r#   r   r_   r   r   r   r   r     s    
c                   @   s   e Zd ZdZd%ddZdd Zdd Zd	d
 Ze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!d" Zd#d$ ZeZdS )'r   )r,   r-   r.   r/   r0   r   c                 C   s6   |dkrt dt | _t | _t | _d | _d S )Nr   zChannels have a maxsize of 1)r   r6   r7   r,   r-   r8   r.   r/   )r"   r:   r   r   r   r!   A  s   


zChannel.__init__c                 C   rP   )Nz<%s at %s %s>rQ   r%   r   r   r   rU   J  rV   zChannel.__repr__c                 C   rW   )Nz<%s %s>rX   r%   r   r   r   rY   M  rZ   zChannel.__str__c                 C   s8   d}| j r|dt| j  7 }| jr|dt| j 7 }|S )Nr\   z getters[%s]z putters[%s])r,   r^   r-   r`   r   r   r   rT   P  s   zChannel._formatc                 C   s   t | jt | j S r   )r^   r-   r,   r%   r   r   r   balanceX  r?   zChannel.balancec                 C   rg   r=   r   r%   r   r   r   rc   \  r   zChannel.qsizec                 C   rg   ri   r   r%   r   r   r   rk   _  r   zChannel.emptyc                 C   rg   ri   r   r%   r   r   r   rl   b  r   zChannel.fullTNc                 C   s   | j t u r| jr| j }|| d S t|sd}t }||f}| j| t	
|t}z*z| jr7|   | }||urFtd|f W n
   t| j|  W |  d S |  w )Nr   z#Invalid switch into Channel.put: %r)r.   r   r,   rF   r$   r   r   r-   rM   r   rn   rm   ro   r	   r   rp   rq   r   r   r   rv   e  s0   

zChannel.putc                 C   s   |  |d d S NFrw   rN   r   r   r   rx     rO   zChannel.put_nowaitc                 C   s   | j t u r| jr| j \}}| j j|j| |S |sd}t }t	|t
}z"z| j| | jr7|   | W W |  S    | j|  |  w r=   )r.   r   r-   rF   r   r   r$   r   r   rn   r   r,   rM   rm   ro   closer   )r"   rr   rs   r   r   ru   r   r   r   ro     s&   


zChannel.getc                 C   r}   r   r~   r%   r   r   r   r     rD   zChannel.get_nowaitc                 C   sT   | j r$| jr(| j }| j  \}}|| || | j r&| jsd S d S d S d S r   )r-   r,   rF   r$   )r"   rt   r   r   r   r   r   r     s   


zChannel._unlockc                 C   r   r   r   r%   r   r   r   rm     r   zChannel._schedule_unlockc                 C   r   r   r   r%   r   r   r   r     r   zChannel.__iter__c                 C   r   r   r   r`   r   r   r   r     r   zChannel.__next__)r   r   )r'   r(   r)   r*   r!   rU   rY   rT   r   r   rc   rk   rl   rv   rx   ro   r   r   rm   r   r   r   r   r   r   r   r   7  s(    
	


c                   C   s
   t   d S r   )r   r   r   r   r   _init  rD   r   )import_c_accelzgevent._queue)6r   
__future__r   r   heapqr   r   r   r   r   r   r6   version_infor
   	__queue__r    r   _Fullr   _Emptygevent.timeoutr   gevent._hub_localr   r8   gevent.exceptionsr	   __all____implements____extensions____imports__hasattrrM   _PySimpleQueuer   r   gevent._waitergevent_waiterr   locals
__import__r   r   objectr   r   r   r   r   r   gevent._utilr   globalsr   r   r   r   <module>   sR   




  DV|