
    \j "                        d Z ddlmZ ddlZddlZddlmZ ddlmZ ddl	m
Z
 erddlZddlmZmZ dd	lmZ dd
lm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   )SoftReadWriteLock)AsyncGeneratorCallable)futures)TracebackTypec                  8    e Zd ZdZddZddZ	 	 	 	 	 	 	 	 ddZy)	$AsyncAcquireSoftReadWriteReturnProxyzTAsync context-aware object that releases an :class:`AsyncSoftReadWriteLock` on exit.c                    || _         y Nlock)selfr   s     B/root/env/lib/python3.12/site-packages/filelock/_soft_rw/_async.py__init__z-AsyncAcquireSoftReadWriteReturnProxy.__init__   s	    	    c                "   K   | j                   S wr   r   r   s    r   
__aenter__z/AsyncAcquireSoftReadWriteReturnProxy.__aenter__   s     yys   c                T   K   | j                   j                          d {    y 7 wr   )r   release)r   exc_type	exc_value	tracebacks       r   	__aexit__z.AsyncAcquireSoftReadWriteReturnProxy.__aexit__   s      ii!!!s   (&(N)r   AsyncSoftReadWriteLockreturnNone)r    r   )r   ztype[BaseException] | Noner   zBaseException | Noner   zTracebackType | Noner    r!   )__name__
__module____qualname____doc__r   r   r    r   r   r   r      s:    ^"," (" (	"
 
"r   r   c            	      8   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
	 ddd	 	 	 	 	 ddZ	 ddd	 	 	 	 	 ddZddd dZedddd!d       Zedddd!d       Zd"dZd#dZy)$r   a  
    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()``.
    Reentrancy, upgrade/downgrade rules, fork handling, heartbeat and TTL stale detection, and singleton
    behavior are delegated to the underlying :class:`SoftReadWriteLock`.

    :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          	     N    t        |||||||      | _        || _        |	| _        y )N)r(   r)   r*   r+   r,   )r   _lock_loop	_executor)
r   	lock_filetimeoutr(   r)   r*   r+   r,   r-   r.   s
             r   r   zAsyncSoftReadWriteLock.__init__;   s5     '%1+'

 
!r   c                .    | j                   j                  S )z>:returns: the path to the lock file passed to the constructor.)r0   r3   r   s    r   r3   z AsyncSoftReadWriteLock.lock_fileT   s     zz###r   c                .    | j                   j                  S )zf:returns: the default timeout applied when ``acquire_read`` / ``acquire_write`` is called without one.)r0   r4   r   s    r   r4   zAsyncSoftReadWriteLock.timeoutY   s     zz!!!r   c                .    | j                   j                  S )zc:returns: whether ``acquire_*`` defaults to blocking; ``False`` makes contention raise immediately.)r0   r(   r   s    r   r(   zAsyncSoftReadWriteLock.blocking^   s     zz"""r   c                    | j                   S )zX:returns: the event loop used for ``run_in_executor``, or ``None`` for the running loop.)r1   r   s    r   r-   zAsyncSoftReadWriteLock.loopc   s     zzr   c                    | j                   S )zZ:returns: the executor used for ``run_in_executor``, or ``None`` for the default executor.)r2   r   s    r   r.   zAsyncSoftReadWriteLock.executorh   s     ~~r   r(   c                  K   | j                  | j                  j                  ||       d{    t        |       S 7 w)aC  
        Acquire a shared read lock.

        See :meth:`SoftReadWriteLock.acquire_read` for the full reentrancy / upgrade / fork semantics. The blocking
        work runs inside ``run_in_executor`` so other coroutines on the same loop continue to progress 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:   Nr   )_runr0   acquire_readr   r   r4   r(   s      r   r=   z#AsyncSoftReadWriteLock.acquire_readm   s;     ( ii

//8iLLL3>> 	M   ,A?Ac                  K   | 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:   Nr   )r<   r0   acquire_writer   r>   s      r   rA   z$AsyncSoftReadWriteLock.acquire_write   s;     & ii

00'HiMMM3>> 	Nr?   Fforcec               n   K   | j                  | j                  j                  |       d{    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``

        rB   N)r<   r0   r   )r   rC   s     r   r   zAsyncSoftReadWriteLock.release   s(      ii

**%i888s   +535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)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)rA   r   r>   s      r   
write_lockz!AsyncSoftReadWriteLock.write_lock   sR        8 <<<	!,,.  	 	= !$,,.  rG   c                j   K   | j                  | j                  j                         d{    y7 w)zRRelease any held lock and release the underlying filesystem resources. Idempotent.N)r<   r0   closer   s    r   rK   zAsyncSoftReadWriteLock.close   s"     ii

(()))s   )313c                   K   | j                   xs t        j                         }|j                  | j                  t        j                  |g|i |       d {   S 7 wr   )r1   asyncioget_running_looprun_in_executorr2   	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"))r3   zstr | os.PathLike[str]r4   floatr(   boolr)   rW   r*   rV   r+   float | Noner,   rV   r-    asyncio.AbstractEventLoop | Noner.   futures.Executor | Noner    r!   )r    str)r    rV   )r    rW   )r    rY   )r    rZ   r   )r4   rX   r(   bool | Noner    r   )rC   rW   r    r!   )r4   rX   r(   r\   r    zAsyncGenerator[None])r    r!   )rR   zCallable[..., object]rS   objectrT   r]   r    r]   )r"   r#   r$   r%   r   propertyr3   r4   r(   r-   r.   r=   rA   r   r   rF   rI   rK   r<   r&   r   r   r   r   %   s   0 "
 !$((,#15,0")" "
 " " "" &" " /" *" 
"2 $ $ " " # #    
 '+?GK?#?9D?	-?0 '+?GK?#?9D?	-?, .3 	9 !W[ ! !" !X\ ! !"*dr   r   )r%   
__future__r   rM   rP   
contextlibr   typingr   _syncr   oscollections.abcr   r	   
concurrentr
   typesr   r   r   __all__r&   r   r   <module>rh      sQ    O "   *   $8"#" "$jd jd\ +r   