
    \j-$                        d Z ddlmZ ddlZddlZddlZddlmZ ddlm	Z	m
Z
mZ ddlmZ e	rddlmZmZ dd	lmZ dd
lmZ  e
d      Z ed      Z G d d      Z G d d      ZddgZy)zIAsync wrapper around :class:`SoftReadWriteLock` for use with ``asyncio``.    )annotationsN)asynccontextmanager)TYPE_CHECKING	ParamSpecTypeVar   )SoftReadWriteLock)AsyncGeneratorCallable)futures)TracebackType_P_Rc            	      @   e Zd ZdZ	 ddddddddd	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 ddZedd       Zedd	       Zedd
       Zedd       Z	edd       Z
edddd d       Zedddd d       Z	 ddd	 	 	 	 	 d!dZ	 ddd	 	 	 	 	 d!dZddd"dZd#dZd#dZd$dZy)%AsyncSoftReadWriteLocka  
    Async wrapper around :class:`SoftReadWriteLock` for ``asyncio`` applications.

    The sync class's blocking filesystem operations run on a thread pool via ``loop.run_in_executor()``. The
    underlying :class:`SoftReadWriteLock` handles reentrancy, upgrade/downgrade rules, fork handling, heartbeat and
    TTL stale detection, and singleton behavior.

    :param lock_file: path to the lock file; sidecar state/write/readers live next to it
    :param timeout: maximum wait time in seconds; ``-1`` means block indefinitely
    :param blocking: if ``False``, raise :class:`~filelock.Timeout` immediately on contention
    :param is_singleton: if ``True``, reuse existing :class:`SoftReadWriteLock` instances per resolved path
    :param heartbeat_interval: seconds between heartbeat refreshes; default 30 s
    :param stale_threshold: seconds of mtime inactivity before a marker is stale; defaults to ``3 * heartbeat_interval``
    :param poll_interval: seconds between acquire retries under contention; default 0.25 s
    :param loop: event loop for ``run_in_executor``; ``None`` uses the running loop
    :param executor: executor for ``run_in_executor``; ``None`` uses the default executor

    .. versionadded:: 3.27.0

    Tg      >@Ng      ?)blockingis_singletonheartbeat_intervalstale_thresholdpoll_intervalloopexecutorc          	         t        j                         | _        t        |||||||      | _        || _        |	| _        y )N)r   r   r   r   r   )osgetpid_creator_pidr	   _lock_loop	_executor)
self	lock_filetimeoutr   r   r   r   r   r   r   s
             K/root/.hermes/venv/lib/python3.12/site-packages/filelock/_soft_rw/_async.py__init__zAsyncSoftReadWriteLock.__init__,   sC     IIK&%1+'

 
!    c                .    | j                   j                  S )z4The path to the lock file passed to the constructor.)r   r!   r    s    r#   r!   z AsyncSoftReadWriteLock.lock_fileF   s     zz###r%   c                .    | j                   j                  S )z\The default timeout applied when ``acquire_read`` / ``acquire_write`` is called without one.)r   r"   r'   s    r#   r"   zAsyncSoftReadWriteLock.timeoutK   s     zz!!!r%   c                .    | j                   j                  S )zYWhether ``acquire_*`` defaults to blocking; ``False`` makes contention raise immediately.)r   r   r'   s    r#   r   zAsyncSoftReadWriteLock.blockingP   s     zz"""r%   c                    | j                   S )zNThe event loop used for ``run_in_executor``, or ``None`` for the running loop.)r   r'   s    r#   r   zAsyncSoftReadWriteLock.loopU   s     zzr%   c                    | j                   S )zPThe executor used for ``run_in_executor``, or ``None`` for the default executor.)r   r'   s    r#   r   zAsyncSoftReadWriteLock.executorZ   s     ~~r%   r   c                 K   | j                  ||       d{    	 d | j                          d{    y7 #7 # | j                          d{  7   w xY ww)a  
        Async context manager that acquires and releases a shared read lock.

        :param timeout: maximum wait time in seconds, or ``None`` to use the instance default
        :param blocking: if ``False``, raise :class:`~filelock.Timeout` immediately; ``None`` uses the instance default

        :raises RuntimeError: if a write lock is already held on this instance
        :raises Timeout: if the lock cannot be acquired within *timeout* seconds

        r,   N)acquire_readreleaser    r"   r   s      r#   	read_lockz AsyncSoftReadWriteLock.read_lock_   sR      (;;;	!,,.  	 	< !$,,.  >   A=AA A?AAAAAAc                 K   | j                  ||       d{    	 d | j                          d{    y7 #7 # | j                          d{  7   w xY ww)a  
        Async context manager that acquires and releases an exclusive write lock.

        :param timeout: maximum wait time in seconds, or ``None`` to use the instance default
        :param blocking: if ``False``, raise :class:`~filelock.Timeout` immediately; ``None`` uses the instance default

        :raises RuntimeError: if a read lock is already held, or a write lock is held by a different thread
        :raises Timeout: if the lock cannot be acquired within *timeout* seconds

        r,   N)acquire_writer/   r0   s      r#   
write_lockz!AsyncSoftReadWriteLock.write_lockq   sR        8 <<<	!,,.  	 	= !$,,.  r2   c                  K   | j                          | j                  | j                  j                  ||       d{    t	        |       S 7 w)a.  
        Acquire a shared read lock.

        See :meth:`SoftReadWriteLock.acquire_read` for reentrancy / upgrade / fork semantics. The blocking work runs
        inside ``run_in_executor`` so other coroutines on the same loop keep progressing while this call waits.

        :param timeout: maximum wait time in seconds, or ``None`` to use the instance default
        :param blocking: if ``False``, raise :class:`~filelock.Timeout` immediately; ``None`` uses the instance default

        :returns: a proxy usable as an async context manager to release the lock

        :raises RuntimeError: if a write lock is already held, if this instance was invalidated by
            :func:`os.fork`, or if :meth:`close` was called
        :raises Timeout: if the lock cannot be acquired within *timeout* seconds

        r,   Nlock)_raise_if_inherited_runr   r.   $AsyncAcquireSoftReadWriteReturnProxyr0   s      r#   r.   z#AsyncSoftReadWriteLock.acquire_read   sG     & 	  "ii

//8iLLL3>> 	M   <AAAc                  K   | j                          | j                  | j                  j                  ||       d{    t	        |       S 7 w)a  
        Acquire an exclusive write lock.

        See :meth:`SoftReadWriteLock.acquire_write` for the two-phase writer-preferring semantics. The blocking work
        runs inside ``run_in_executor``.

        :param timeout: maximum wait time in seconds, or ``None`` to use the instance default
        :param blocking: if ``False``, raise :class:`~filelock.Timeout` immediately; ``None`` uses the instance default

        :returns: a proxy usable as an async context manager to release the lock

        :raises RuntimeError: if a read lock is already held, if a write lock is held by a different thread, if
            this instance was invalidated by :func:`os.fork`, or if :meth:`close` was called
        :raises Timeout: if the lock cannot be acquired within *timeout* seconds

        r,   Nr7   )r9   r:   r   r4   r;   r0   s      r#   r4   z$AsyncSoftReadWriteLock.acquire_write   sG     & 	  "ii

00'HiMMM3>> 	Nr<   Fforcec                  K   | j                   t        j                         k(  r0| j                  | j                  j
                  |       d{    yy7 w)z
        Release one level of the current lock.

        :param force: if ``True``, release the lock completely regardless of the current lock level

        :raises RuntimeError: if no lock is currently held and *force* is ``False``

        r>   N)r   r   r   r:   r   r/   )r    r?   s     r#   r/   zAsyncSoftReadWriteLock.release   sD      		+))DJJ..e)<<< ,<s   AAAAc                   K   | j                   t        j                         k(  r.| j                  | j                  j
                         d{    yy7 w)zRRelease any held lock and release the underlying filesystem resources. Idempotent.N)r   r   r   r:   r   closer'   s    r#   rB   zAsyncSoftReadWriteLock.close   s>     		+))DJJ,,--- ,-s   A
AAAc                |    | j                   t        j                         k7  rd| j                   d}t	        |      y )NzAsyncSoftReadWriteLock on z4 was inherited across fork; construct a new instance)r   r   r   r!   RuntimeError)r    msgs     r#   r9   z*AsyncSoftReadWriteLock._raise_if_inherited   s:    		+.t~~.>>rsCs## ,r%   c                   K   | j                   xs t        j                         }|j                  | j                  t        j                  |g|i |       d {   S 7 wN)r   asyncioget_running_looprun_in_executorr   	functoolspartial)r    funcargskwargsr   s        r#   r:   zAsyncSoftReadWriteLock._run   sQ     zz7W557))$..):K:KD:bSW:b[a:bccccs   AA"A A"))r!   zstr | os.PathLike[str]r"   floatr   boolr   rR   r   rQ   r   float | Noner   rQ   r    asyncio.AbstractEventLoop | Noner   futures.Executor | NonereturnNone)rV   str)rV   rQ   )rV   rR   )rV   rT   )rV   rU   rG   )r"   rS   r   bool | NonerV   zAsyncGenerator[None])r"   rS   r   rY   rV   r;   )r?   rR   rV   rW   )rV   rW   )rM   zCallable[_P, _R]rN   z_P.argsrO   z	_P.kwargsrV   r   )__name__
__module____qualname____doc__r$   propertyr!   r"   r   r   r   r   r1   r5   r.   r4   r/   rB   r9   r:    r%   r#   r   r      s   0 "
 !$((,#15,0")" "
 " " "" &" " /" *" 
"4 $ $ " " # #     !W[ ! !" !X\ ! !$ '+?GK?#?9D?	-?0 '+?GK?#?9D?	-?. .3 
=.
$
dr%   r   c                  8    e Zd ZdZddZddZ	 	 	 	 	 	 	 	 ddZy)	r;   zTAsync context-aware object that releases an :class:`AsyncSoftReadWriteLock` on exit.c                    || _         y rG   r7   )r    r8   s     r#   r$   z-AsyncAcquireSoftReadWriteReturnProxy.__init__   s	    	r%   c                "   K   | j                   S wrG   r7   r'   s    r#   
__aenter__z/AsyncAcquireSoftReadWriteReturnProxy.__aenter__   s     yys   c                T   K   | j                   j                          d {    y 7 wrG   )r8   r/   )r    exc_type	exc_value	tracebacks       r#   	__aexit__z.AsyncAcquireSoftReadWriteReturnProxy.__aexit__   s      ii!!!s   (&(N)r8   r   rV   rW   )rV   r   )re   ztype[BaseException] | Nonerf   zBaseException | Nonerg   zTracebackType | NonerV   rW   )rZ   r[   r\   r]   r$   rc   rh   r_   r%   r#   r;   r;      s:    ^"," (" (	"
 
"r%   r;   )r]   
__future__r   rH   rK   r   
contextlibr   typingr   r   r   _syncr	   collections.abcr
   r   
concurrentr   typesr   r   r   r   r;   __all__r_   r%   r#   <module>rq      sf    O "   	 * 4 4 $8"#t_T]sd sdl" "& +r%   