o z¾nh«ã@sbdZddlmZddlmZddlmZgd¢ZGdd„deƒZ Gd d „d eƒZ Gd d „d ƒZ d S)aðAn adaptation of Py2.3/2.4's Queue module which supports reentrant behavior, using RLock instead of Lock for its mutex object. The Queue object is used exclusively by the sqlalchemy.pool.QueuePool class. This is to support the connection pool's usage of weakref callbacks to return connections to the underlying Queue, which can in extremely rare cases be invoked within the ``get()`` method of the Queue itself, producing a ``put()`` inside the ``get()`` and therefore a reentrant condition. é)Údeque)Útimeé)Ú threading)ÚEmptyÚFullÚQueuec@óeZdZdZdS)rz4Exception raised by Queue.get(block=0)/get_nowait().N©Ú__name__Ú __module__Ú __qualname__Ú__doc__©rrú@/usr/local/lib/python3.10/dist-packages/sqlalchemy/util/queue.pyrórc@r )rz4Exception raised by Queue.put(block=0)/put_nowait().Nr rrrrr$rrc@s‚eZdZd!dd„Zdd„Zdd„Zd d „Zd"d d„Zdd„Zd"dd„Z dd„Z dd„Z dd„Z dd„Z dd„Zdd„Zdd „Zd S)#rrFcCs:| |¡t ¡|_t |j¡|_t |j¡|_||_dS)z½Initialize a queue object with a given maximum size. If `maxsize` is <= 0, the queue size is infinite. If `use_lifo` is True, this Queue acts like a Stack (LIFO). N)Ú_initrÚRLockÚmutexÚ ConditionÚ not_emptyÚnot_fullÚuse_lifo)ÚselfÚmaxsizerrrrÚ__init__+s   zQueue.__init__cCó |j ¡| ¡}|j ¡|S)z9Return the approximate size of the queue (not reliable!).)rÚacquireÚ_qsizeÚrelease©rÚnrrrÚqsizeBs  z Queue.qsizecCr)zKReturn True if the queue is empty, False otherwise (not reliable!).)rrÚ_emptyrr rrrÚemptyJó  z Queue.emptycCr)zJReturn True if the queue is full, False otherwise (not reliable!).)rrÚ_fullrr rrrÚfullSr%z Queue.fullTNcCs¾|j ¡zS|s| ¡rt‚n8|dur!| ¡r |j ¡| ¡sn&|dkr)tdƒ‚tƒ|}| ¡rG|tƒ}|dkr=t‚|j |¡| ¡s2| |¡|j  ¡W|j  ¡dS|j  ¡w)aPut 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 positive 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). Nrú#'timeout' must be a positive numberç) rrr&rÚwaitÚ ValueErrorÚ_timeÚ_putrÚnotifyr)rÚitemÚblockÚtimeoutÚendtimeÚ remainingrrrÚput\s. ÿ ÿ€   ü  z Queue.putcCs | |d¡S)z­Put 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©rr/rrrÚ put_nowaits zQueue.put_nowaitcCs¼|j ¡zR|s| ¡rt‚n8|dur!| ¡r |j ¡| ¡sn&|dkr)tdƒ‚tƒ|}| ¡rG|tƒ}|dkr=t‚|j |¡| ¡s2| ¡}|j  ¡|W|j  ¡S|j  ¡w)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 ``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). Nrr(r)) rrr#rr*r+r,Ú_getrr.r)rr0r1r2r3r/rrrÚget‡s0 ÿ ÿ€   ü z Queue.getcCs | d¡S)z¯Remove and return an item from the queue without blocking. Only get an item if one is immediately available. Otherwise raise the ``Empty`` exception. F)r8©rrrrÚ get_nowait©s zQueue.get_nowaitcCs||_tƒ|_dS©N)rrÚqueue)rrrrrr·s z Queue._initcCs t|jƒSr;)Úlenr<r9rrrr»s z Queue._qsizecCs|j Sr;)r<r9rrrr#¿sz Queue._emptycCs|jdko t|jƒ|jkS)Nr)rr=r<r9rrrr&Ãsz Queue._fullcCs|j |¡dSr;)r<Úappendr5rrrr-Çsz Queue._putcCs|jr|j ¡S|j ¡Sr;)rr<ÚpopÚpopleftr9rrrr7Ës  z Queue._get)rF)TN)r r r rr"r$r'r4r6r8r:rrr#r&r-r7rrrrr*s  # " rN) rÚ collectionsrrr,ÚcompatrÚ__all__Ú ExceptionrrrrrrrÚs