U kf'@s6dZddlmZddlZddlmZddlZddlmZddl Z ddl m Z ddl m Z ddl m Z dd l m Z dd l mZdd l mZd d lmZd dlmZd dlmZede dZdddgZGdddeZGdddeZGddde eZGdddeeZGdddeeZGdddeeZdS)aAn 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. ) annotationsN)deque)time)Any) Awaitable)Deque)Generic)Optional)TypeVar)await_fallback) await_only)memoized_property_T)boundEmptyFullQueuec@seZdZdZdS)rz4Exception raised by Queue.get(block=0)/get_nowait().N__name__ __module__ __qualname____doc__rrE/opt/hc_python/lib64/python3.8/site-packages/sqlalchemy/util/queue.pyr,sc@seZdZdZdS)rz4Exception raised by Queue.put(block=0)/put_nowait().Nrrrrrr2sc@seZdZUded<ded<d!ddddd Zdd d d Zdd d dZdd ddZdddddZd"dddddddZ dd ddZ d#dddddd Z dS)$ QueueCommonintmaxsizebooluse_liforFrrcCsdSNrselfrrrrr__init__<zQueueCommon.__init__returncCs tdSr!NotImplementedErrorr#rrrempty>szQueueCommon.emptycCs tdSr!r(r*rrrfullAszQueueCommon.fullcCs tdSr!r(r*rrrqsizeDszQueueCommon.qsizerNoneitemr'cCs tdSr!r(r#r0rrr put_nowaitGszQueueCommon.put_nowaitTNOptional[float]r0blocktimeoutr'cCs tdSr!r()r#r0r5r6rrrputJszQueueCommon.putcCs tdSr!r(r*rrr get_nowaitOszQueueCommon.get_nowaitr5r6r'cCs tdSr!r()r#r5r6rrrgetRszQueueCommon.get)rF)TN)TN) rrr__annotations__r$r+r,r-r2r7r8r:rrrrr8s rc@seZdZUded<d.ddddd Zdd d d Zdd d dZdd ddZd/dddddddZdddddZ d0ddddddZ dd dd Z ddd!d"d#Z dd d$d%Z dd d&d'Zdd d(d)Zdddd*d+Zdd d,d-ZdS)1rz Deque[_T]queuerFrrr cCs:||t|_t|j|_t|j|_||_dS)zInitialize 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)_init threadingRLockmutex Condition not_emptynot_fullrr"rrrr$Ys   zQueue.__init__r&c Cs&|j|W5QRSQRXdS)z9Return the approximate size of the queue (not reliable!).N)r@_qsizer*rrrr-psz Queue.qsizec Cs&|j|W5QRSQRXdS)zKReturn True if the queue is empty, False otherwise (not reliable!).N)r@_emptyr*rrrr+vsz Queue.emptyc Cs&|j|W5QRSQRXdS)zJReturn True if the queue is full, False otherwise (not reliable!).N)r@_fullr*rrrr,}sz Queue.fullTNrr3r.r4c Cs|j|s|r~tnd|dkr8|r~|jq"nF|dkrHtdt|}|r~|t}|dkrpt|j|qR|||jW5QRXdS)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) rCrFrwait ValueError_time_putrBnotify)r#r0r5r6endtime remainingrrrr7s"   z Queue.putr/cCs ||dS)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)r7r1rrrr2szQueue.put_nowaitr9c Cs|j|s|r~tnd|dkr8|r~|jq"nF|dkrHtdt|}|r~|t}|dkrpt|j|qR|}|j|W5QRSQRXdS)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). NrrGrH) rBrErrIrJrK_getrCrM)r#r5r6rNrOr0rrrr:s$    z Queue.getcCs |dS)zRemove and return an item from the queue without blocking. Only get an item if one is immediately available. Otherwise raise the ``Empty`` exception. F)r:r*rrrr8szQueue.get_nowait)rr'cCs||_t|_dSr!)rrr<)r#rrrrr=sz Queue._initcCs t|jSr!)lenr<r*rrrrDsz Queue._qsizecCs|j Sr!)r<r*rrrrEsz Queue._emptycCs|jdkot|j|jkS)Nr)rrQr<r*rrrrFsz Queue._fullcCs|j|dSr!)r<appendr1rrrrLsz Queue._putcCs|jr|jS|jSdSr!)rr<poppopleftr*rrrrPs z Queue._get)rF)TN)TN)rrrr;r$r-r+r,r7r2r:r8r=rDrErFrLrPrrrrrVs" "  c@seZdZejr$edddddZneeZd&dd d d d Zd d ddZ ddZ ddZ e dd ddZ dddddZd'dd ddddd Zdd d!d"Zd(d ddd#d$d%ZdS))AsyncAdaptedQueuezAwaitable[Any]r) coroutiner'cCsdSr!r)rVrrrawait_szAsyncAdaptedQueue.await_rFrrr cCs||_||_dSr!)rrr"rrrr$szAsyncAdaptedQueue.__init__r&cCs |jSr!)_queuer+r*rrrr+szAsyncAdaptedQueue.emptycCs |jSr!)rXr,r*rrrr,szAsyncAdaptedQueue.fullcCs |jSr!)rXr-r*rrrr-szAsyncAdaptedQueue.qsizezasyncio.Queue[_T]cCs(|jrtj|jd}ntj|jd}|S)N)r)rasyncio LifoQueuerr)r#r<rrrrXs zAsyncAdaptedQueue._queuer.r/c CsBz|j|Wn,tjk r<}z t|W5d}~XYnXdSr!)rXr2rY QueueFullr)r#r0errrrrr2szAsyncAdaptedQueue.put_nowaitTNr3r4c Cs|s||Sz:|dk r4|t|j||n||j|Wn2tjtjfk rz}z t|W5d}~XYnXdSr!) r2rWrYwait_forrXr7r[ TimeoutErrorr)r#r0r5r6r\rrrr7s zAsyncAdaptedQueue.putc Cs>z |jWStjk r8}z t|W5d}~XYnXdSr!)rXr8rY QueueEmptyr)r#r\rrrr8+s zAsyncAdaptedQueue.get_nowaitr9c Cs||s |Sz8|dk r0|t|j|WS||jWSWn2tjtjfk rv}z t|W5d}~XYnXdSr!) r8rWrYr]rXr:r_r^r)r#r5r6r\rrrr:1szAsyncAdaptedQueue.get)rF)TN)TN)rrrtyping TYPE_CHECKING staticmethodrWr r$r+r,r-rrXr2r7r8r:rrrrrUs rUc@seZdZejseeZdS)FallbackAsyncAdaptedQueueN)rrrr`rarbr rWrrrrrc@src)r __future__rrY collectionsrr>rrKr`rrrrr r Z concurrencyr r Z langhelpersrr__all__ ExceptionrrrrrUrcrrrr s0              P