-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
388c37a
commit a9cbabe
Showing
3 changed files
with
46 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,4 +13,4 @@ | |
# limitations under the License. | ||
|
||
|
||
__version__ = "4.0.1" | ||
__version__ = "4.0.2" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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())) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters