o
    RhT                     @   s`  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	m
Z
mZ d dlmZ d dlmZ d dlmZmZmZmZmZmZmZmZ d d	lmZ d
ZdZedZee Z G dd dee Z!G dd de!e ee Z"G dd de!e ee Z#G dd dee Z$G dd de"e Z%G dd de#e Z&G dd de$e Z'G dd de$e Z(dS )    N)
QueueEmpty)	QueueFull)deque)heappopheappush)Empty)Full)AnyCallableDequeGenericListOptionalSetTypeVar)Protocolz1.0.0)QueuePriorityQueue	LifoQueue	SyncQueue
AsyncQueue	BaseQueueTc                   @   s   e Zd ZedefddZedefddZdddZdefd	d
Z	edefddZ
defddZdefddZdeddfddZdefddZdS )r   returnc                 C      d S N selfr   r   P/home/air/sanwanet/backup_V2/venv/lib/python3.10/site-packages/janus/__init__.pymaxsize      zBaseQueue.maxsizec                 C   r   r   r   r   r   r   r   closed"   r!   zBaseQueue.closedNc                 C   r   r   r   r   r   r   r   	task_done&      zBaseQueue.task_donec                 C   r   r   r   r   r   r   r   qsize)   r$   zBaseQueue.qsizec                 C   r   r   r   r   r   r   r   unfinished_tasks,   r!   zBaseQueue.unfinished_tasksc                 C   r   r   r   r   r   r   r   empty0   r$   zBaseQueue.emptyc                 C   r   r   r   r   r   r   r   full3   r$   zBaseQueue.fullitemc                 C   r   r   r   r   r)   r   r   r   
put_nowait6   r$   zBaseQueue.put_nowaitc                 C   r   r   r   r   r   r   r   
get_nowait9   r$   zBaseQueue.get_nowaitr   N)__name__
__module____qualname__propertyintr    boolr"   r#   r%   r&   r'   r(   r   r+   r,   r   r   r   r   r      s    
r   c                	   @   s   e Zd ZedefddZedefddZdddZdefd	d
Z	edefddZ
defddZdefddZdeddfddZdefddZd dedededdfddZd dededefddZdddZdS )!r   r   c                 C   r   r   r   r   r   r   r   r    >   r!   zSyncQueue.maxsizec                 C   r   r   r   r   r   r   r   r"   B   r!   zSyncQueue.closedNc                 C   r   r   r   r   r   r   r   r#   F   r$   zSyncQueue.task_donec                 C   r   r   r   r   r   r   r   r%   I   r$   zSyncQueue.qsizec                 C   r   r   r   r   r   r   r   r&   L   r!   zSyncQueue.unfinished_tasksc                 C   r   r   r   r   r   r   r   r'   P   r$   zSyncQueue.emptyc                 C   r   r   r   r   r   r   r   r(   S   r$   zSyncQueue.fullr)   c                 C   r   r   r   r*   r   r   r   r+   V   r$   zSyncQueue.put_nowaitc                 C   r   r   r   r   r   r   r   r,   Y   r$   zSyncQueue.get_nowaitTblocktimeoutc                 C   r   r   r   )r   r)   r4   r5   r   r   r   put\   r$   zSyncQueue.putc                 C   r   r   r   )r   r4   r5   r   r   r   get_   r$   zSyncQueue.getc                 C   r   r   r   r   r   r   r   joinb   r$   zSyncQueue.joinr-   TN)r.   r/   r0   r1   r2   r    r3   r"   r#   r%   r&   r'   r(   r   r+   r,   OptFloatr6   r7   r8   r   r   r   r   r   =   s     
r   c                   @   s6   e Zd ZdeddfddZdefddZd
dd	ZdS )r   r)   r   Nc                       d S r   r   r*   r   r   r   r6   g      zAsyncQueue.putc                    r;   r   r   r   r   r   r   r7   j   r<   zAsyncQueue.getc                    r;   r   r   r   r   r   r   r8   m   r<   zAsyncQueue.joinr-   )r.   r/   r0   r   r6   r7   r8   r   r   r   r   r   f   s    r   c                   @   s  e Zd Zd+deddfddZd,ddZd,d	d
ZedefddZ	edefddZ
ed-ddZed.ddZdeddfddZdefddZdeddfddZdefddZdeddfddZd,d d!Zd,d"d#Zd$eddfd%d&Zd$eddfd'd(Zd,d)d*ZdS )/r   r   r    r   Nc                    s"  t   _| _ | d _t  _t	 j _
t	 j _t	 j _t   _tjd d dkrBt jddd   t 	 j _t 	 j _t   _ j  d _t  _dtd	 d
tdd f fdd}| _dtd	 d
tdd f fdd}| _t  _t  _d S )Nr      )r=   
   r   	_get_loopc                   S   r   r   r   r   r   r   r   <lambda>   s    z Queue.__init__.<locals>.<lambda>Fcallback).Nargsr   c                    s0   z j j| g|R   W d S  ty   Y d S w r   )_loopcall_soon_threadsafeRuntimeErrorrA   rB   r   r   r   checked_call_soon_threadsafe   s
   z4Queue.__init__.<locals>.checked_call_soon_threadsafec                    s&    j  s j j| g|R   d S d S r   )rC   	is_closed	call_soonrF   r   r   r   checked_call_soon   s   
z)Queue.__init__.<locals>.checked_call_soon) asyncioget_running_looprC   _maxsize_init_unfinished_tasks	threadingLock_sync_mutex	Condition_sync_not_empty_sync_not_full_all_tasks_done_async_mutexsysversion_infogetattr_async_not_empty_async_not_fullEvent	_finishedset_closing_pendingr
   r	   _call_soon_threadsafe
_call_soon_SyncQueueProxy_sync_queue_AsyncQueueProxy_async_queue)r   r    rG   rJ   r   r   r   __init__r   s:   





	
zQueue.__init__c                 C   sZ   | j   d| _| jD ]}|  q
| j  | j  W d    d S 1 s&w   Y  d S )NT)rR   r`   ra   cancelr^   r_   rV   
notify_all)r   futr   r   r   close   s   


"zQueue.closec                    s@   | j stdtdI d H  | jsd S t| jI d H  d S )NzWaiting for non-closed queuer   )r`   rE   rK   sleepra   waitr   r   r   r   wait_closed   s   zQueue.wait_closedc                 C   s   | j o| j S r   )r`   ra   r   r   r   r   r"      s   zQueue.closedc                 C      | j S r   )rM   r   r   r   r   r          zQueue.maxsize_SyncQueueProxy[T]c                 C   rp   r   )re   r   r   r   r   sync_q   rq   zQueue.sync_q_AsyncQueueProxy[T]c                 C   rp   r   )rg   r   r   r   r   async_q   rq   zQueue.async_qc                 C   s   t  | _d S r   )r   _queuer   r    r   r   r   rN      s   zQueue._initc                 C   
   t | jS r   lenrv   r   r   r   r   _qsize      
zQueue._qsizer)   c                 C      | j | d S r   rv   appendr*   r   r   r   _put      z
Queue._putc                 C   
   | j  S r   )rv   popleftr   r   r   r   _get   r|   z
Queue._getc                 C   s&   |  | |  jd7  _| j  d S )N   )r   rO   r^   clearr*   r   r   r   _put_internal   s   
zQueue._put_internalc                    s    d fdd} j d | d S )Nr   c                      6    j   j  W d    d S 1 sw   Y  d S r   )rR   rT   notifyr   r   r   r   f      "z'Queue._notify_sync_not_empty.<locals>.fr-   )rC   run_in_executor)r   r   r   r   r   _notify_sync_not_empty   s   zQueue._notify_sync_not_emptyc                    s@   d fdd}t  jd |}| jj  j| d S )Nr   c                      r   r   )rR   rU   r   r   r   r   r   r      r   z&Queue._notify_sync_not_full.<locals>.fr-   )rK   ensure_futurerC   r   add_done_callbackra   discardadd)r   r   rk   r   r   r   _notify_sync_not_full   s   zQueue._notify_sync_not_full
threadsafec                   >   dfdd d fdd}|r | d S | d S )Nr   c                	      L    j 4 I d H   j  W d   I d H  d S 1 I d H sw   Y  d S r   )rW   r[   r   r   r   r   r   r         .z(Queue._notify_async_not_empty.<locals>.fc                     ,   j   } | jj j|  d S r   rC   create_taskr   ra   r   r   taskr   r   r   r   
task_maker      z1Queue._notify_async_not_empty.<locals>.task_makerr-   rb   rc   r   r   r   r   r   r   _notify_async_not_empty   
   zQueue._notify_async_not_emptyc                   r   )Nr   c                	      r   r   )rW   r\   r   r   r   r   r   r      r   z'Queue._notify_async_not_full.<locals>.fc                     r   r   r   r   r   r   r   r     r   z0Queue._notify_async_not_full.<locals>.task_makerr-   r   r   r   r   r   _notify_async_not_full   r   zQueue._notify_async_not_fullc                 C   s   | j rtdd S )Nz*Operation on the closed queue is forbidden)r`   rE   r   r   r   r   _check_closing  s   zQueue._check_closing)r   r-   )r   rr   )r   rt   )r.   r/   r0   r2   rh   rl   ro   r1   r3   r"   r    rs   ru   rN   r{   r   r   r   r   r   r   r   r   r   r   r   r   r   r   q   s,    
-


	r   c                	   @   s   e Zd ZdZdee fddZedefddZ	ede
fdd	Zd#ddZd#ddZdefddZedefddZde
fddZde
fddZd$dede
dedd
fddZd$de
dedefddZdedd
fdd Zdefd!d"Zd
S )%rd   jCreate a queue object with a given maximum size.

    If maxsize is <= 0, the queue size is infinite.
    parentc                 C   
   || _ d S r   _parentr   r   r   r   r   rh     r|   z_SyncQueueProxy.__init__r   c                 C      | j jS r   r   rM   r   r   r   r   r         z_SyncQueueProxy.maxsizec                 C   r   r   r   r"   r   r   r   r   r"     r   z_SyncQueueProxy.closedNc                 C   s   | j   | j j/ | j jd }|dkr,|dk rtd| j j  | j j| j jj	 || j _W d   dS 1 s;w   Y  dS )a.  Indicate that a formerly enqueued task is complete.

        Used by Queue consumer threads.  For each get() used to fetch a task,
        a subsequent call to task_done() tells the queue that the processing
        on the task is complete.

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

        Raises a ValueError if called more times than there were items
        placed in the queue.
        r   r   !task_done() called too many timesN)
r   r   rV   rO   
ValueErrorrj   rC   rD   r^   r_   )r   
unfinishedr   r   r   r#   !  s   


"z_SyncQueueProxy.task_donec                 C   sn   | j   | j j$ | j jr%| j j  | j   | j jsW d   dS W d   dS 1 s0w   Y  dS )a  Blocks 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 task_done()
        to indicate the item was retrieved and all work on it is complete.

        When the count of unfinished tasks drops to zero, join() unblocks.
        N)r   r   rV   rO   rn   r   r   r   r   r8   9  s   
	


"z_SyncQueueProxy.joinc                 C   r   )z9Return the approximate size of the queue (not reliable!).r   r{   r   r   r   r   r%   H     
z_SyncQueueProxy.qsizec                 C   r   z&Return the number of unfinished tasks.r   rO   r   r   r   r   r&   L     z _SyncQueueProxy.unfinished_tasksc                 C   s   | j   S )a  Return True if the queue is empty, False otherwise (not reliable!).

        This method is likely to be removed at some point.  Use qsize() == 0
        as a direct substitute, but be aware that either approach risks a race
        condition where a queue can grow before the result of empty() or
        qsize() can be used.

        To create code that needs to wait for all queued tasks to be
        completed, the preferred technique is to use the join() method.
        r   r   r   r   r   r'   Q  s   z_SyncQueueProxy.emptyc                 C   s"   d| j j  k o| j  kS   S )aO  Return True if the queue is full, False otherwise (not reliable!).

        This method is likely to be removed at some point.  Use qsize() >= n
        as a direct substitute, but be aware that either approach risks a race
        condition where a queue can shrink before the result of full() or
        qsize() can be used.
        r   )r   rM   r{   r   r   r   r   r(   ^  s   "z_SyncQueueProxy.fullTr)   r4   r5   c                 C   s,  | j   | j j | j jdkrq|s| j  | j jkrtnS|du r;| j  | j jkr:| j j  | j  | j jks+n6|dk rCtd| j jj	}| | }| j  | j jkrq||  }|dkrat| j j| | j  | j jksV| j 
| | j j  | j jdd W d   dS 1 sw   Y  dS )a  Put an item into the queue.

        If optional args 'block' is true and 'timeout' is None (the default),
        block if necessary until a free slot is available. If 'timeout' is
        a non-negative number, it blocks at most 'timeout' seconds and raises
        the 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 Full exception ('timeout'
        is ignored in that case).
        r   N''timeout' must be a non-negative number        Tr   )r   r   rU   rM   r{   SyncQueueFullrn   r   rC   timer   rT   r   r   )r   r)   r4   r5   r   endtime	remainingr   r   r   r6   h  s4   




"z_SyncQueueProxy.putc                 C   s   | j   | j ji |s| j  stnC|du r)| j  s(| j j  | j  rn.|dk r1td| j jj}| | }| j  sW||  }|dkrKt| j j| | j  r@| j 	 }| j j
  | j jdd |W  d   S 1 suw   Y  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 non-negative number, it blocks at most 'timeout' seconds and raises
        the 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 Empty exception ('timeout' is ignored
        in that case).
        Nr   r   r   Tr   )r   r   rT   r{   SyncQueueEmptyrn   r   rC   r   r   rU   r   r   )r   r4   r5   r   r   r   r)   r   r   r   r7     s4   










$z_SyncQueueProxy.getc                 C   s   | j |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 Full exception.
        Fr4   )r6   r*   r   r   r   r+     s   z_SyncQueueProxy.put_nowaitc                 C   s   | j ddS )zRemove and return an item from the queue without blocking.

        Only get an item if one is immediately available. Otherwise
        raise the Empty exception.
        Fr   )r7   r   r   r   r   r,     s   z_SyncQueueProxy.get_nowaitr-   r9   )r.   r/   r0   __doc__r   r   rh   r1   r2   r    r3   r"   r#   r8   r%   r&   r'   r(   r:   r6   r7   r+   r,   r   r   r   r   rd     s$    


""rd   c                   @   s   e Zd ZdZdee fddZedefddZ	de
fdd	Zede
fd
dZede
fddZdefddZdefddZdeddfddZdeddfddZdefddZdefddZd ddZd ddZdS )!rf   r   r   c                 C   r   r   r   r   r   r   r   rh     r|   z_AsyncQueueProxy.__init__r   c                 C   r   r   r   r   r   r   r   r"     r   z_AsyncQueueProxy.closedc                 C   r   )zNumber of items in the queue.r   r   r   r   r   r%     r   z_AsyncQueueProxy.qsizec                 C   r   r   r   r   r   r   r   r&     r   z!_AsyncQueueProxy.unfinished_tasksc                 C   r   )z%Number of items allowed in the queue.r   r   r   r   r   r      r   z_AsyncQueueProxy.maxsizec                 C   s   |   dkS )z3Return True if the queue is empty, False otherwise.r   )r%   r   r   r   r   r'     s   z_AsyncQueueProxy.emptyc                 C   s    | j jdkrdS |  | j jkS )zReturn True if there are maxsize items in the queue.

        Note: if the Queue was initialized with maxsize=0 (the default),
        then full() is never True.
        r   F)r   rM   r%   r   r   r   r   r(     s   z_AsyncQueueProxy.fullr)   Nc              	      s
  | j   | j j4 I dH j | j j  d}zK| j jdkrHd}|rH| j  | j jk}|rFd}| j j  | j j I dH  | j j  d}|s"| j 	| | j j
  | j   W |rb| j j  n
|rl| j j  w w W d  I dH  dS 1 I dH s~w   Y  dS )zPut an item into the queue.

        Put an item into the queue. If the queue is full, wait until a free
        slot is available before adding item.

        This method is a coroutine.
        NTr   F)r   r   r\   rR   acquirerM   r{   releasern   r   r[   r   r   )r   r)   lockeddo_waitr   r   r   r6     s8   
	.z_AsyncQueueProxy.putc                 C   s~   | j   | j j, | j jdkr| j  | j jkrt| j | | j jdd | j   W d   dS 1 s8w   Y  dS )zyPut an item into the queue without blocking.

        If no free slot is immediately available, raise QueueFull.
        r   Fr   N)	r   r   rR   rM   r{   AsyncQueueFullr   r   r   r*   r   r   r   r+     s   

"z_AsyncQueueProxy.put_nowaitc              	      s   | j   | j j4 I dH a | j j  d}zMd}|r@| j  dk}|r>d}| j j  | j j I dH  | j j  d}|s| j  }| j j	
  | j   |W |rZ| j j  W  d  I dH  S |rn| j j  w w 1 I dH suw   Y  dS )zRemove and return an item from the queue.

        If queue is empty, wait until an item is available.

        This method is a coroutine.
        NTr   F)r   r   r[   rR   r   r{   r   rn   r   r\   r   r   )r   r   r   r)   r   r   r   r7     s6   



z_AsyncQueueProxy.getc                 C   sn   | j   | j j$ | j  dkrt| j  }| j jdd | j   |W  d   S 1 s0w   Y  dS )zRemove and return an item from the queue.

        Return an item if one is immediately available, else raise QueueEmpty.
        r   Fr   N)r   r   rR   r{   AsyncQueueEmptyr   r   r   r*   r   r   r   r,   4  s   



$z_AsyncQueueProxy.get_nowaitc                 C   s   | j   | j j5 | j jdkrtd| j  jd8  _| j jdkr6| j j  | j j  W d   dS W d   dS 1 sAw   Y  dS )a$  Indicate that a formerly enqueued task is complete.

        Used by queue consumers. For each get() used to fetch a task,
        a subsequent call to task_done() tells the queue that the processing
        on the task is complete.

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

        Raises ValueError if called more times than there were items placed in
        the queue.
        r   r   r   N)r   r   rV   rO   r   r^   r_   rj   r   r   r   r   r#   C  s   

"z_AsyncQueueProxy.task_donec                    sh   	 | j j | j   | j jdkr	 W d   dS W d   n1 s%w   Y  | j j I dH  q)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 calls 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, join() unblocks.
        Tr   N)r   rR   r   rO   r^   rn   r   r   r   r   r8   Z  s   

z_AsyncQueueProxy.joinr-   )r.   r/   r0   r   r   r   rh   r1   r3   r"   r2   r%   r&   r    r'   r(   r6   r+   r7   r,   r#   r8   r   r   r   r   rf     s$    
rf   c                   @   sP   e Zd ZdZdeddfddZdefddZd	eddfd
dZdefddZ	dS )r   zVariant of Queue that retrieves open entries in priority order
    (lowest first).

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

    r    r   Nc                 C   s
   g | _ d S r   )_heap_queuerw   r   r   r   rN   r  r|   zPriorityQueue._initc                 C   rx   r   )rz   r   r   r   r   r   r{   u  r|   zPriorityQueue._qsizer)   c                 C   s   t | j| d S r   )r   r   r*   r   r   r   r   x  r   zPriorityQueue._putc                 C   rx   r   )r   r   r   r   r   r   r   {  r|   zPriorityQueue._get)
r.   r/   r0   r   r2   rN   r{   r   r   r   r   r   r   r   r   j  s    r   c                   @   s>   e Zd ZdZdefddZdeddfddZdefd	d
ZdS )r   zBVariant of Queue that retrieves most recently added entries first.r   c                 C   rx   r   ry   r   r   r   r   r{     r|   zLifoQueue._qsizer)   Nc                 C   r}   r   r~   r*   r   r   r   r     r   zLifoQueue._putc                 C   r   r   )rv   popr   r   r   r   r     r|   zLifoQueue._get)	r.   r/   r0   r   r2   r{   r   r   r   r   r   r   r   r     s
    r   ))rK   rX   rP   r   r   r   r   collectionsr   heapqr   r   queuer   r   r   r   typingr	   r
   r   r   r   r   r   r   typing_extensionsr   __version____all__r   floatr:   r   r   r   r   rd   rf   r   r   r   r   r   r   <module>   s4    (
 )   . .