Skip to content

Commit

Permalink
Exposed ResourceGuard in the public API (#630)
Browse files Browse the repository at this point in the history
Closes #627.
  • Loading branch information
agronholm authored Nov 5, 2023
1 parent ec4a4b4 commit 9f8e45c
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 2 deletions.
1 change: 1 addition & 0 deletions docs/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ Synchronization
.. autoclass:: anyio.Condition
.. autoclass:: anyio.Semaphore
.. autoclass:: anyio.CapacityLimiter
.. autoclass:: anyio.ResourceGuard

.. autoclass:: anyio.LockStatistics
.. autoclass:: anyio.EventStatistics
Expand Down
19 changes: 19 additions & 0 deletions docs/synchronization.rst
Original file line number Diff line number Diff line change
Expand Up @@ -163,3 +163,22 @@ Example::

You can adjust the total number of tokens by setting a different value on the limiter's
``total_tokens`` property.

Resource guards
---------------

Some resources, such as sockets, are very sensitive about concurrent use and should not
allow even attempts to be used concurrently. For such cases, :class:`ResourceGuard` is
the appropriate solution::

class Resource:
def __init__(self):
self._guard = ResourceGuard()

async def do_something() -> None:
with self._guard:
...

Now, if another task tries calling the ``do_something()`` method on the same
``Resource`` instance before the first call has finished, that will raise a
:exc:`BusyResourceError`.
1 change: 1 addition & 0 deletions docs/versionhistory.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ This library adheres to `Semantic Versioning 2.0 <http://semver.org/>`_.
instead of ``cancellable``
- Removed a checkpoint when exiting a task group
- Bumped minimum version of trio to v0.23
- Exposed the ``ResourceGuard`` class in the public API

**4.0.0**

Expand Down
1 change: 1 addition & 0 deletions src/anyio/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
from ._core._synchronization import EventStatistics as EventStatistics
from ._core._synchronization import Lock as Lock
from ._core._synchronization import LockStatistics as LockStatistics
from ._core._synchronization import ResourceGuard as ResourceGuard
from ._core._synchronization import Semaphore as Semaphore
from ._core._synchronization import SemaphoreStatistics as SemaphoreStatistics
from ._core._tasks import TASK_STATUS_IGNORED as TASK_STATUS_IGNORED
Expand Down
15 changes: 13 additions & 2 deletions src/anyio/_core/_synchronization.py
Original file line number Diff line number Diff line change
Expand Up @@ -483,10 +483,21 @@ def statistics(self) -> CapacityLimiterStatistics:


class ResourceGuard:
"""
A context manager for ensuring that a resource is only used by a single task at a
time.
Entering this context manager while the previous has not exited it yet will trigger
:exc:`BusyResourceError`.
:param action: the action to guard against (visible in the :exc:`BusyResourceError`
when triggered, e.g. "Another task is already {action} this resource")
"""

__slots__ = "action", "_guarded"

def __init__(self, action: str):
self.action = action
def __init__(self, action: str = "using"):
self.action: str = action
self._guarded = False

def __enter__(self) -> None:
Expand Down

0 comments on commit 9f8e45c

Please sign in to comment.