Skip to content

Commit 0ff2eb5

Browse files
authored
remove: 🔥 Lock overuse
1 parent bf23661 commit 0ff2eb5

File tree

5 files changed

+19
-50
lines changed

5 files changed

+19
-50
lines changed

‎injection/common/event.py‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
from typing import ContextManager
55
from weakref import WeakSet
66

7-
from injection.common.tools.threading import frozen_collection
8-
97
__all__ = ("Event", "EventChannel", "EventListener")
108

119

@@ -28,7 +26,7 @@ class EventChannel:
2826
@contextmanager
2927
def dispatch(self, event: Event) -> ContextManager | ContextDecorator:
3028
with ExitStack() as stack:
31-
for listener in frozen_collection(self.__listeners):
29+
for listener in tuple(self.__listeners):
3230
context_manager = listener.on_event(event)
3331

3432
if context_manager is None:

‎injection/common/lazy.py‎

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
from types import MappingProxyType
33
from typing import Generic, TypeVar
44

5-
from injection.common.tools.threading import thread_lock
6-
75
__all__ = ("Lazy", "LazyMapping")
86

97
_T = TypeVar("_T")
@@ -26,11 +24,9 @@ def is_set(self) -> bool:
2624

2725
def __setup_cache(self, factory: Callable[[], _T]):
2826
def new_cache() -> Iterator[_T]:
29-
with thread_lock:
30-
self.__is_set = True
31-
3227
nonlocal factory
3328
cached = factory()
29+
self.__is_set = True
3430
del factory
3531

3632
while True:

‎injection/common/queue.py‎

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
from dataclasses import dataclass, field
55
from typing import NoReturn, Protocol, TypeVar
66

7-
from injection.common.tools.threading import thread_lock
8-
97
__all__ = ("LimitedQueue",)
108

119
_T = TypeVar("_T")
@@ -58,9 +56,7 @@ def __next__(self) -> _T:
5856
try:
5957
return next(self.__queue)
6058
except StopIteration as exc:
61-
with thread_lock:
62-
self.__queue = NoQueue()
63-
59+
self.__queue = NoQueue()
6460
raise exc
6561

6662
def add(self, item: _T):
Lines changed: 8 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,13 @@
1-
from collections.abc import Callable, Collection, Iterator
2-
from functools import wraps
1+
from contextlib import ContextDecorator, contextmanager
32
from threading import RLock
4-
from typing import Any, TypeVar
3+
from typing import ContextManager
54

6-
__all__ = ("frozen_collection", "synchronized", "thread_lock")
5+
__all__ = ("synchronized",)
76

8-
_T = TypeVar("_T")
9-
thread_lock = RLock()
7+
__thread_lock = RLock()
108

119

12-
def synchronized(function: Callable[..., Any] = None, /):
13-
def decorator(fn):
14-
@wraps(fn)
15-
def wrapper(*args, **kwargs):
16-
with thread_lock:
17-
return fn(*args, **kwargs)
18-
19-
return wrapper
20-
21-
return decorator(function) if function else decorator
22-
23-
24-
def frozen_collection(collection: Collection[_T]) -> Iterator[_T]:
25-
with thread_lock:
26-
t = tuple(collection)
27-
28-
yield from t
10+
@contextmanager
11+
def synchronized() -> ContextManager | ContextDecorator:
12+
with __thread_lock:
13+
yield

‎injection/core/module.py‎

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,7 @@
3939
from injection.common.event import Event, EventChannel, EventListener
4040
from injection.common.lazy import Lazy, LazyMapping
4141
from injection.common.queue import LimitedQueue
42-
from injection.common.tools.threading import (
43-
frozen_collection,
44-
synchronized,
45-
thread_lock,
46-
)
42+
from injection.common.tools.threading import synchronized
4743
from injection.common.tools.type import find_types, format_type, get_origins
4844
from injection.exceptions import (
4945
InjectionError,
@@ -193,7 +189,7 @@ def get_instance(self) -> _T:
193189
with suppress(KeyError):
194190
return self.cache[self.__INSTANCE_KEY]
195191

196-
with thread_lock:
192+
with synchronized():
197193
instance = self.factory()
198194
self.cache[self.__INSTANCE_KEY] = instance
199195

@@ -273,7 +269,7 @@ def __injectables(self) -> frozenset[Injectable]:
273269
def update(self, classes: Iterable[type], injectable: Injectable, override: bool):
274270
classes = frozenset(get_origins(*classes))
275271

276-
with thread_lock:
272+
with synchronized():
277273
if not injectable:
278274
classes -= self.__classes
279275
override = True
@@ -289,7 +285,7 @@ def update(self, classes: Iterable[type], injectable: Injectable, override: bool
289285

290286
return self
291287

292-
@synchronized
288+
@synchronized()
293289
def unlock(self):
294290
for injectable in self.__injectables:
295291
injectable.unlock()
@@ -360,7 +356,7 @@ def is_locked(self) -> bool:
360356

361357
@property
362358
def __brokers(self) -> Iterator[Broker]:
363-
yield from frozen_collection(self.__modules)
359+
yield from tuple(self.__modules)
364360
yield self.__container
365361

366362
def injectable(
@@ -502,7 +498,7 @@ def change_priority(self, module: Module, priority: ModulePriority):
502498

503499
return self
504500

505-
@synchronized
501+
@synchronized()
506502
def unlock(self):
507503
for broker in self.__brokers:
508504
broker.unlock()
@@ -697,7 +693,7 @@ def bind(
697693
return Arguments(bound.args, bound.kwargs)
698694

699695
def update(self, module: Module):
700-
with thread_lock:
696+
with synchronized():
701697
self.__dependencies = Dependencies.resolve(
702698
self.signature,
703699
module,
@@ -730,7 +726,5 @@ def __consume_setup_queue(self):
730726
return self
731727

732728
def __set_signature(self, signature: Signature):
733-
with thread_lock:
734-
self.__signature__ = signature
735-
729+
self.__signature__ = signature
736730
return self

0 commit comments

Comments
 (0)