This commit is contained in:
Todd Gamblin 2023-02-23 02:46:03 -08:00
parent 457ea43e95
commit 6b38f2203d
No known key found for this signature in database
GPG Key ID: C16729F1AACF66C6

View File

@ -3,12 +3,14 @@
# #
# SPDX-License-Identifier: (Apache-2.0 OR MIT) # SPDX-License-Identifier: (Apache-2.0 OR MIT)
import contextlib
import errno import errno
import os import os
import socket import socket
import sys import sys
import time import time
from datetime import datetime from datetime import datetime
from typing import Callable, Optional, Union
import llnl.util.tty as tty import llnl.util.tty as tty
from llnl.util.lang import pretty_seconds from llnl.util.lang import pretty_seconds
@ -546,11 +548,11 @@ def upgrade_read_to_write(self, timeout=None):
else: else:
raise LockUpgradeError(self.path) raise LockUpgradeError(self.path)
def release_read(self, release_fn=None): def release_read(self, release_fn: Callable = None):
"""Releases a read lock. """Releases a read lock.
Arguments: Arguments:
release_fn (typing.Callable): function to call *before* the last recursive release_fn (Callable): function to call *before* the last recursive
lock (read or write) is released. lock (read or write) is released.
If the last recursive lock will be released, then this will call If the last recursive lock will be released, then this will call
@ -586,7 +588,7 @@ def release_write(self, release_fn=None):
"""Releases a write lock. """Releases a write lock.
Arguments: Arguments:
release_fn (typing.Callable): function to call before the last recursive release_fn (Callable): function to call before the last recursive
write is released. write is released.
If the last recursive *write* lock will be released, then this If the last recursive *write* lock will be released, then this
@ -684,18 +686,6 @@ def _status_msg(self, locktype, status):
class LockTransaction(object): class LockTransaction(object):
"""Simple nested transaction context manager that uses a file lock. """Simple nested transaction context manager that uses a file lock.
Arguments:
lock (Lock): underlying lock for this transaction to be accquired on
enter and released on exit
acquire (typing.Callable or contextlib.contextmanager): function to be called
after lock is acquired, or contextmanager to enter after acquire and leave
before release.
release (typing.Callable): function to be called before release. If
``acquire`` is a contextmanager, this will be called *after*
exiting the nexted context and before the lock is released.
timeout (float): number of seconds to set for the timeout when
accquiring the lock (default no timeout)
If the ``acquire_fn`` returns a value, it is used as the return value for If the ``acquire_fn`` returns a value, it is used as the return value for
``__enter__``, allowing it to be passed as the ``as`` argument of a ``__enter__``, allowing it to be passed as the ``as`` argument of a
``with`` statement. ``with`` statement.
@ -709,7 +699,27 @@ class LockTransaction(object):
""" """
def __init__(self, lock, acquire=None, release=None, timeout=None): def __init__(
self,
lock: Lock,
acquire: Optional[Union[Callable, contextlib.contextmanager]] = None,
release: Optional[Callable] = None,
timeout: Optional[float] = None,
):
"""
Arguments:
lock: underlying lock for this transaction to be accquired on
enter and released on exit
acquire: function to be called
after lock is acquired, or contextmanager to enter after acquire and leave
before release.
release: function to be called before release. If
``acquire`` is a contextmanager, this will be called *after*
exiting the nexted context and before the lock is released.
timeout: number of seconds to set for the timeout when
accquiring the lock (default no timeout)
"""
self._lock = lock self._lock = lock
self._timeout = timeout self._timeout = timeout
self._acquire_fn = acquire self._acquire_fn = acquire