Skip to content

Commit

Permalink
version
Browse files Browse the repository at this point in the history
  • Loading branch information
mikeshardmind committed Apr 5, 2024
1 parent 388c37a commit a9cbabe
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 42 deletions.
2 changes: 1 addition & 1 deletion async_utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@
# limitations under the License.


__version__ = "4.0.1"
__version__ = "4.0.2"
82 changes: 41 additions & 41 deletions async_utils/keyed_locks.py
Original file line number Diff line number Diff line change
@@ -1,41 +1,41 @@
# Copyright 2020-present Michael Hall
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


from __future__ import annotations

import asyncio
from collections.abc import Hashable
from typing import Generic, TypeVar
from weakref import WeakValueDictionary

__all__ = ["KeyedLocks"]

KT = TypeVar("KT", bound=Hashable)


class KeyedLocks(Generic[KT]):
"""Locks per hashable resource type
Currently implemented with a weakvalue dictionary + asyncio.Locks
implementation could be improved to not rely on weakreference in the future
for performance reasons, but this would likely involve also re-implementing
some of the functionality of asyncio locks. May revisit later, intent here
is that if I do, everything I use like this improves at once.
"""

def __init__(self) -> None:
self._locks: WeakValueDictionary[KT, asyncio.Lock] = WeakValueDictionary()

def __getitem__(self, item: KT) -> asyncio.Lock:
return self._locks.get(item, self._locks.setdefault(item, asyncio.Lock()))
# Copyright 2020-present Michael Hall
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


from __future__ import annotations

import asyncio
from collections.abc import Hashable
from typing import Generic, TypeVar
from weakref import WeakValueDictionary

__all__ = ["KeyedLocks"]

KT = TypeVar("KT", bound=Hashable)


class KeyedLocks(Generic[KT]):
"""Locks per hashable resource type
Currently implemented with a weakvalue dictionary + asyncio.Locks
implementation could be improved to not rely on weakreference in the future
for performance reasons, but this would likely involve also re-implementing
some of the functionality of asyncio locks. May revisit later, intent here
is that if I do, everything I use like this improves at once.
"""

def __init__(self) -> None:
self._locks: WeakValueDictionary[KT, asyncio.Lock] = WeakValueDictionary()

def __getitem__(self, item: KT) -> asyncio.Lock:
return self._locks.get(item, self._locks.setdefault(item, asyncio.Lock()))
4 changes: 4 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ reportUnnecessaryTypeIgnoreComment = true
line-length = 130
target-version = "py310"

[tool.ruff.format]
line-ending = "lf"


[tool.ruff.lint]
select = [
"F", "E", "I", "UP", "YTT", "ANN", "S", "BLE", "B", "A", "COM", "C4", "DTZ",
Expand Down

0 comments on commit a9cbabe

Please sign in to comment.