L i'hdZddlmZddlZddlmZddlZddlmZddl Z ddl m Z ddl m Z ddl m Z dd l m Z dd l mZdd l mZd d lmZd dlmZd dlmZede ZgdZGddeZGddeZGdde eZGddeeZGddeeZGddeeZy)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)bound)EmptyFullQueueceZdZdZy)rz4Exception raised by Queue.get(block=0)/get_nowait().N__name__ __module__ __qualname____doc__[/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/sqlalchemy/util/queue.pyrr,:rrceZdZdZy)rz4Exception raised by Queue.put(block=0)/put_nowait().Nrrrrrr2rrrcxeZdZUded<ded<dddZddZddZddZdd Z d dd Z dd Z ddd Z y ) QueueCommonintmaxsizebooluse_lifocyNrselfr#r%s r__init__zQueueCommon.__init__<srctr'NotImplementedErrorr)s remptyzQueueCommon.empty> !##rctr'r,r.s rfullzQueueCommon.fullAr0rctr'r,r.s rqsizezQueueCommon.qsizeDr0rctr'r,r)items r put_nowaitzQueueCommon.put_nowaitGr0rNctr'r,)r)r7blocktimeouts rputzQueueCommon.putJs "##rctr'r,r.s r get_nowaitzQueueCommon.get_nowaitOr0rctr'r,)r)r:r;s rgetzQueueCommon.getRr0rrFr#r"r%r$returnr$rDr"r7rrDNoneTNr7rr:r$r;Optional[float]rDrGrDrr:r$r;rJrDr) rrr__annotations__r*r/r2r4r8r<r>r@rrrr!r!8sY LNE$$$$HL$$#$5D$ $ $$rr!ceZdZUded<dddZddZddZddZ d ddZdd Z ddd Z dd Z dd Z dd Z ddZddZddZddZy)rz Deque[_T]queuec|j|tj|_tj|j|_tj|j|_||_y)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_fullr%r(s rr*zQueue.__init__YsW 7 __& #,,TZZ8"++DJJ7   rcf|j5|jcdddS#1swYyxYw)z9Return the approximate size of the queue (not reliable!).N)rT_qsizer.s rr4z Queue.qsizeps)ZZ !;;= ! ! !'0cf|j5|jcdddS#1swYyxYw)zKReturn True if the queue is empty, False otherwise (not reliable!).N)rT_emptyr.s rr/z Queue.emptyvs)ZZ !;;= ! ! !rZcf|j5|jcdddS#1swYyxYw)zJReturn True if the queue is full, False otherwise (not reliable!).N)rT_fullr.s rr2z Queue.full}s)ZZ ::<   rZNc*|j5|s|jrt|<|jr|jj|jr+nq|dkr t dt |z}|jrD|t z }|dkrt|jj||jrD|j ||jjdddy#1swYyxYw)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) rWr^rwait ValueError_time_putrVnotify)r)r7r:r;endtime remainings rr<z Queue.puts]] $::<JjjlMM&&(jjlQ;$%JKK'G+jjl '%' 1I C'" MM&&y1 jjl IIdO NN ! ! ## $ $ $sAD #A1D +D  Dc&|j|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)r<r6s rr8zQueue.put_nowaits xxe$$rc,|j5|s|jrt|<|jr|jj|jr+nq|dkr t dt |z}|jrD|t z }|dkrt|jj||jrD|j }|jj|cdddS#1swYyxYw)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`ra) rVr\rrbrcrd_getrWrf)r)r:r;rgrhr7s rr@z Queue.gets^^ ;;=KkkmNN'')kkmQ;$%JKK'G+kkm '%' 1I C'# NN'' 2 kkm 99;D MM "%   sAD #A1D +D  Dc$|jdS)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.s rr>zQueue.get_nowaitsxxrc0||_t|_yr')r#rrO)r)r#s rrQz Queue._inits W rc,t|jSr')lenrOr.s rrYz Queue._qsizes4::rc|j Sr')rOr.s rr\z Queue._emptys::~rch|jdkDxr"t|j|jk(S)Nr)r#rorOr.s rr^z Queue._fulls'||aCC Ot||$CCrc:|jj|yr')rOappendr6s rrez Queue._puts $rc|jr|jjS|jjSr')r%rOpoppopleftr.s rrkz Queue._gets/ ==::>># #::%%' 'rrArBrErCrHrIrFrLrK)r#r"rDrG)rrrrMr*r4r/r2r<r8r@r>rQrYr\r^rerkrrrrrVsx !.! ! HL $ $# $5D $  $D%@D (rrceZdZejr ed dZneeZd ddZddZ dZ dZ e ddZ ddZ d dd Zdd Zddd Zy)AsyncAdaptedQueuecyr'r) coroutines rawait_zAsyncAdaptedQueue.await_s58rc ||_||_yr')r%r#r(s rr*zAsyncAdaptedQueue.__init__s   rc6|jjSr')_queuer/r.s rr/zAsyncAdaptedQueue.empty{{  ""rc6|jjSr')r~r2r.s rr2zAsyncAdaptedQueue.fulls{{!!rc6|jjSr')r~r4r.s rr4zAsyncAdaptedQueue.qsizerrc|jr"tj|j}|Stj|j}|S)N)r#)r%asyncio LifoQueuer#r)r)rOs rr~zAsyncAdaptedQueue._queues? ==%%dll;E MM$,,7E rc |jj|y#tj$r}t |d}~wwxYwr')r~r8r QueueFullr)r)r7errs rr8zAsyncAdaptedQueue.put_nowaits9 " KK " "4 (   "&c ! "sA <ANcn|s|j|S |?|jtj|jj ||y|j|jj |y#tj tjf$r}t|d}~wwxYwr') r8r{rwait_forr~r<r TimeoutErrorr)r)r7r:r;rs rr<zAsyncAdaptedQueue.puts??4( ( "" G,,T[[__T-BGLM DKKOOD12!!7#7#78 "&c ! "sAB*B#B4$ B//B4c |jjS#tj$r}t |d}~wwxYwr')r~r>r QueueEmptyr)r)rs rr>zAsyncAdaptedQueue.get_nowait+s: #;;))+ +!! #'s " #s? :?cd|s|jS |=|jtj|jj |S|j|jj S#tj tjf$r}t|d}~wwxYwr') r>r{rrr~r@rrr)r)r:r;rs rr@zAsyncAdaptedQueue.get1s??$ $ #"{{$$T[[__%6@{{4;;??#455""G$8$89 #'s " #s>A<(A<<#B/ B**B/)rzzAwaitable[Any]rDrrArBrC)rDzasyncio.Queue[_T]rFrHrIrKrL)rrrtyping TYPE_CHECKING staticmethodr{rr*r/r2r4rr~r8r<r>r@rrrrxrxs  8 8j)#"# "HL " "# "5D "  "# #rrxc6eZdZejs eeZyy)FallbackAsyncAdaptedQueueN)rrrrrrr r{rrrrr@s   n- rr)r __future__rr collectionsrrRrrdrrrrr r r concurrencyr r langhelpersrr__all__ Exceptionrrr!rrxrrrrrs # '#* T $ I  9 $'"+$