L i0dZddlmZddlZddlZddlZddlZddlZddlm Z ddl m Z ddl m Z ddlmZmZmZmZdd lmZmZmZdd lmZdd lmZdd lmZdd lmZer2ddlZddl m!Z!ddl"m#Z#ddl$m%Z%ejLdk\rddlm'Z'nddl(m'Z'ejRdZ*e GddeZ+Gdde+e Z,GddZ-GddeZ.Gddee.Z/Gdd ee/Z0Gd!d"ee/Z1Gd#d$ee/Z2gd%Z3y)&z1An asyncio-based implementation of the file lock.) annotationsN) dataclass)iscoroutinefunction)local) TYPE_CHECKINGAnyNoReturncast) BaseFileLockFileLockContext FileLockMeta)Timeout) SoftFileLock) UnixFileLock)WindowsFileLock)Callable)futures) TracebackType) )Selffilelockc<eZdZUdZdZded<dZded<dZded <y) AsyncFileLockContextzGA dataclass which holds the context for a ``BaseAsyncFileLock`` object.Tboolrun_in_executorNfutures.Executor | Noneexecutor asyncio.AbstractEventLoop | Noneloop)__name__ __module__ __qualname____doc__r__annotations__rr!V/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/filelock/asyncio.pyrr$s+Q!OT )-H%,.2D *1r(rceZdZdZy)AsyncThreadLocalFileContextz8A thread local version of the ``FileLockContext`` class.Nr"r#r$r%r'r(r)r+r+2sBr(r+c8eZdZdZddZddZ ddZy) AsyncAcquireReturnProxyzDA context-aware object that will release the lock file when exiting.c||_yNlock)selfr2s r)__init__z AsyncAcquireReturnProxy.__init__9s  r(c"K|jSwr0r1r3s r) __aenter__z"AsyncAcquireReturnProxy.__aenter__<syys cTK|jjd{y7wr0)r2releaser3exc_type exc_value tracebacks r) __aexit__z!AsyncAcquireReturnProxy.__aexit__?s ii!!!s (&(N)r2BaseAsyncFileLockreturnNone)r@r?r;ztype[BaseException] | Noner<zBaseException | Noner=zTracebackType | Noner@rA)r"r#r$r%r4r7r>r'r(r)r.r.6s:N","("( "  "r(r.c\eZdZ ddddddd dfdZxZS)AsyncFileLockMetaFTNblocking is_singletonr!rrc n|r|r d} t| t | |||||||||  } td| S)Nz:run_in_executor is not supported when thread_local is True) lock_filetimeoutmode thread_localrFrGr!rrr?) ValueErrorsuper__call__r ) clsrIrJrKrLrFrGr!rrmsginstance __class__s r)rOzAsyncFileLockMeta.__call__IsX ONCS/ !7#%%+$  '22r(iF)rIstr | os.PathLike[str]rJfloatrKintrLrrFrrGrr!r rrrrr@r?)r"r#r$rO __classcell__)rSs@r)rDrDHs" 3"15 $,03)33 3  333/33*3 33r(rDceZdZdZ ddddddd ddZeddZeddZejdd Zedd Z ddd  dd Z ddd Z ddZ ddZ ddZ d dZd!dZy)"r?z'Base class for asynchronous file locks.FTNrEc||_||_tj||||||| d} |rtnt di| |_y)a" Create a new lock object. :param lock_file: path to the file :param timeout: default timeout when acquiring the lock, in seconds. It will be used as fallback value in the acquire method, if no timeout value (``None``) is given. If you want to disable the timeout, set it to a negative value. A timeout of 0 means that there is exactly one attempt to acquire the file lock. :param mode: file permissions for the lockfile :param thread_local: Whether this object's internal context should be thread local or not. If this is set to ``False`` then the lock will be reentrant across threads. :param blocking: whether the lock should be blocking or not :param is_singleton: If this is set to ``True`` then only one instance of this class will be created per lock file. This is useful if you want to use the lock object for reentrant locking without needing to pass the same object around. :param loop: The event loop to use. If not specified, the running event loop will be used. :param run_in_executor: If this is set to ``True`` then the lock will be acquired in an executor. :param executor: The executor to use. If not specified, the default executor will be used. )rIrJrKrFr!rrNr')_is_thread_local _is_singletonosfspathr+r_context) r3rIrJrKrLrFrGr!rrkwargss r)r4zBaseAsyncFileLock.__init__js[@!-) 9- . " 0ul/J`t/ /  r(c.|jjS)z"::return: whether run in executor.)r`rr6s r)rz!BaseAsyncFileLock.run_in_executors}},,,r(c.|jjS)z::return: the executor.r`rr6s r)rzBaseAsyncFileLock.executors}}%%%r(c&||j_y)z Change the executor. :param value: the new executor or ``None`` :type value: futures.Executor | None Nrd)r3values r)rzBaseAsyncFileLock.executors"' r(c.|jjS)z::return: the event loop.)r`r!r6s r)r!zBaseAsyncFileLock.loops}}!!!r()rFcK||jj}||jj}|jxjdz c_t |}|j }t j} |js:tjd|||j|jd{|jrtjd||n|dur"tjd||t|d|cxkrt j|z kr%nn"tjd||t|d }tj||||tj|d{ t%| S77#t $r4t#d|jjdz |j_wxYww) aW Try to acquire the file lock. :param timeout: maximum wait time for acquiring the lock, ``None`` means use the default :attr:`~BaseFileLock.timeout` is and if ``timeout < 0``, there is no timeout and this method will block until the lock could be acquired :param poll_interval: interval of trying to acquire the lock file :param blocking: defaults to True. If False, function will return immediately if it cannot obtain a lock on the first attempt. Otherwise, this method will block until the timeout expires or the lock is acquired. :raises Timeout: if fails to acquire lock within the timeout period :return: a context object that will unlock the file when the context is exited .. code-block:: python # You can use this method in the context manager (recommended) with lock.acquire(): pass # Or use an equivalent try-finally construct: lock.acquire() try: pass finally: lock.release() Nr z#Attempting to acquire lock %s on %szLock %s acquired on %sFz+Failed to immediately acquire lock %s on %srz"Timeout on acquiring lock %s on %sz2Lock %s not acquired on %s, waiting %s seconds ...r1)r`rJrF lock_counteridrItime perf_counter is_locked_LOGGERdebug_run_internal_method_acquirerasynciosleep BaseExceptionmaxr.)r3rJ poll_intervalrFlock_id lock_filename start_timerQs r)acquirezBaseAsyncFileLock.acquiresD ?mm++G  }}--H ""a'"T( &&(  ~~MM"GR_`33DMMBBB>>MM":G]Su$MM"OQXZgh!-00B$"3"3"5 "BBMM"FQ^_!-00J c7M=ImmM222 'D11!C3 ),Q 0J0JQ0N)ODMM &  sDA;G>AFFCFFF GFF=GGcK|jr|jxjdzc_|jjdk(s|rzt||j}}t j d|||j|jd{d|j_t j d||yyy7/w)a+ Releases the file lock. Please note, that the lock is only completely released, if the lock counter is 0. Also note, that the lock file itself is not automatically deleted. :param force: If true, the lock counter is ignored and the lock is released in every case/ r rz#Attempting to release lock %s on %sNzLock %s released on %s) rmr`rirjrIrnrorp_release)r3forcerwrxs r)r9zBaseAsyncFileLock.releases >> MM & &! + &}}))Q.%)+D4>> CWm\// >>>-. * 6O 38 ?sBCC0CcKt|r|d{y|jrG|jxstj}|j|j |d{y|y7`7wr0)rrr!rrget_running_loopr)r3methodr!s r)rpz&BaseAsyncFileLock._run_internal_method sa v &(NN  ! !99: 8 8 :D&&t}}f= = = H  >s"A=A9AA=+A;,A=;A=cd}t|)z Replace old __enter__ method to avoid using it. NOTE: DO NOT USE `with` FOR ASYNCIO LOCKS, USE `async with` INSTEAD. :return: none :rtype: NoReturn z>Do not use `with` for asyncio locks, use `async with` instead.)NotImplementedError)r3rQs r) __enter__zBaseAsyncFileLock.__enter__sO!#&&r(cBK|jd{|S7w)zF Acquire the lock. :return: the lock object N)rzr6s r)r7zBaseAsyncFileLock.__aenter__"s!lln  s c@K|jd{y7w)z Release the lock. :param exc_type: the exception type if raised :param exc_value: the exception value if raised :param traceback: the exception traceback if raised N)r9r:s r)r>zBaseAsyncFileLock.__aexit__,sllns cJtjt5|jxst j }|j s"|j|jdn!|j|jddddy#1swYyxYw)z'Called when the lock object is deleted.T)r}N) contextlibsuppress RuntimeErrorr!rrr is_runningrun_until_completer9 create_task)r3r!s r)__del__zBaseAsyncFileLock.__del__<sx   . ;99: 8 8 :D??$'' 4 (@A  D!9:  ; ; ;s A6BB"rT)rIrVrJrWrKrXrLrrFrrGrr!r rrrrr@rA)r@r)r@r)rfrr@rA)r@r )Ng?)rJz float | NonervrWrFz bool | Noner@r.)F)r}rr@rA)rzCallable[[], Any]r@rA)r@r )r@rrB)r@rA)r"r#r$r%r4propertyrrsetterr!rzr9rprr7r>rr'r(r)r?r?gsk1 " 0 "15 $,00 )0 0  0  0 0 0 /0 0 *0  0 d--&&__''"" !%#B2 !% B2B2B2  B2 ! B2HP& ',((    ;r(r?) metaclassceZdZdZy)AsyncSoftFileLockz.Simply watches the existence of the lock file.Nr,r'r(r)rrFs8r(rceZdZdZy)AsyncUnixFileLockzHUses the :func:`fcntl.flock` to hard lock the lock file on unix systems.Nr,r'r(r)rrJsRr(rceZdZdZy)AsyncWindowsFileLockzNUses the :func:`msvcrt.locking` to hard lock the lock file on windows systems.Nr,r'r(r)rrNsXr(r)r.rrrr?)4r% __future__rrrrloggingr^rk dataclassesrinspectr threadingrtypingrrr r _apir r r_errorr_softr_unixr_windowsrsyscollections.abcr concurrentrtypesr version_infortyping_extensions getLoggerrnrr+r.rDr?rrr__all__r'r(r)rs7" !'55==%("# 7"* '  J '  2? 2  2C"6C""$3 3>\; 0A\;~9 &79S &7SY?,=Y r(