ez-a-sync 0.32.9__cp39-cp39-macosx_11_0_arm64.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of ez-a-sync might be problematic. Click here for more details.

Files changed (177) hide show
  1. a_sync/ENVIRONMENT_VARIABLES.py +42 -0
  2. a_sync/__init__.pxd +2 -0
  3. a_sync/__init__.py +144 -0
  4. a_sync/_smart.c +22623 -0
  5. a_sync/_smart.cpython-39-darwin.so +0 -0
  6. a_sync/_smart.pxd +2 -0
  7. a_sync/_smart.pyi +202 -0
  8. a_sync/_smart.pyx +652 -0
  9. a_sync/_typing.py +258 -0
  10. a_sync/a_sync/__init__.py +60 -0
  11. a_sync/a_sync/_descriptor.c +19745 -0
  12. a_sync/a_sync/_descriptor.cpython-39-darwin.so +0 -0
  13. a_sync/a_sync/_descriptor.pyi +34 -0
  14. a_sync/a_sync/_descriptor.pyx +422 -0
  15. a_sync/a_sync/_flags.c +5687 -0
  16. a_sync/a_sync/_flags.cpython-39-darwin.so +0 -0
  17. a_sync/a_sync/_flags.pxd +3 -0
  18. a_sync/a_sync/_flags.pyx +92 -0
  19. a_sync/a_sync/_helpers.c +13718 -0
  20. a_sync/a_sync/_helpers.cpython-39-darwin.so +0 -0
  21. a_sync/a_sync/_helpers.pxd +3 -0
  22. a_sync/a_sync/_helpers.pyi +10 -0
  23. a_sync/a_sync/_helpers.pyx +167 -0
  24. a_sync/a_sync/_kwargs.c +10672 -0
  25. a_sync/a_sync/_kwargs.cpython-39-darwin.so +0 -0
  26. a_sync/a_sync/_kwargs.pxd +2 -0
  27. a_sync/a_sync/_kwargs.pyx +64 -0
  28. a_sync/a_sync/_meta.py +210 -0
  29. a_sync/a_sync/abstract.c +11350 -0
  30. a_sync/a_sync/abstract.cpython-39-darwin.so +0 -0
  31. a_sync/a_sync/abstract.pyi +141 -0
  32. a_sync/a_sync/abstract.pyx +221 -0
  33. a_sync/a_sync/base.c +14066 -0
  34. a_sync/a_sync/base.cpython-39-darwin.so +0 -0
  35. a_sync/a_sync/base.pyi +60 -0
  36. a_sync/a_sync/base.pyx +271 -0
  37. a_sync/a_sync/config.py +168 -0
  38. a_sync/a_sync/decorator.py +562 -0
  39. a_sync/a_sync/flags.c +4471 -0
  40. a_sync/a_sync/flags.cpython-39-darwin.so +0 -0
  41. a_sync/a_sync/flags.pxd +72 -0
  42. a_sync/a_sync/flags.pyi +74 -0
  43. a_sync/a_sync/flags.pyx +72 -0
  44. a_sync/a_sync/function.c +37747 -0
  45. a_sync/a_sync/function.cpython-39-darwin.so +0 -0
  46. a_sync/a_sync/function.pxd +24 -0
  47. a_sync/a_sync/function.pyi +556 -0
  48. a_sync/a_sync/function.pyx +1363 -0
  49. a_sync/a_sync/method.c +28387 -0
  50. a_sync/a_sync/method.cpython-39-darwin.so +0 -0
  51. a_sync/a_sync/method.pxd +2 -0
  52. a_sync/a_sync/method.pyi +524 -0
  53. a_sync/a_sync/method.pyx +999 -0
  54. a_sync/a_sync/modifiers/__init__.pxd +1 -0
  55. a_sync/a_sync/modifiers/__init__.py +101 -0
  56. a_sync/a_sync/modifiers/cache/__init__.py +160 -0
  57. a_sync/a_sync/modifiers/cache/memory.py +165 -0
  58. a_sync/a_sync/modifiers/limiter.py +132 -0
  59. a_sync/a_sync/modifiers/manager.c +15262 -0
  60. a_sync/a_sync/modifiers/manager.cpython-39-darwin.so +0 -0
  61. a_sync/a_sync/modifiers/manager.pxd +5 -0
  62. a_sync/a_sync/modifiers/manager.pyi +219 -0
  63. a_sync/a_sync/modifiers/manager.pyx +296 -0
  64. a_sync/a_sync/modifiers/semaphores.py +173 -0
  65. a_sync/a_sync/property.c +27952 -0
  66. a_sync/a_sync/property.cpython-39-darwin.so +0 -0
  67. a_sync/a_sync/property.pyi +376 -0
  68. a_sync/a_sync/property.pyx +813 -0
  69. a_sync/a_sync/singleton.py +63 -0
  70. a_sync/aliases.py +3 -0
  71. a_sync/async_property/__init__.pxd +1 -0
  72. a_sync/async_property/__init__.py +1 -0
  73. a_sync/async_property/cached.c +20542 -0
  74. a_sync/async_property/cached.cpython-39-darwin.so +0 -0
  75. a_sync/async_property/cached.pxd +10 -0
  76. a_sync/async_property/cached.pyi +45 -0
  77. a_sync/async_property/cached.pyx +178 -0
  78. a_sync/async_property/proxy.c +36410 -0
  79. a_sync/async_property/proxy.cpython-39-darwin.so +0 -0
  80. a_sync/async_property/proxy.pxd +2 -0
  81. a_sync/async_property/proxy.pyi +124 -0
  82. a_sync/async_property/proxy.pyx +474 -0
  83. a_sync/asyncio/__init__.pxd +6 -0
  84. a_sync/asyncio/__init__.py +164 -0
  85. a_sync/asyncio/as_completed.c +18198 -0
  86. a_sync/asyncio/as_completed.cpython-39-darwin.so +0 -0
  87. a_sync/asyncio/as_completed.pxd +8 -0
  88. a_sync/asyncio/as_completed.pyi +109 -0
  89. a_sync/asyncio/as_completed.pyx +269 -0
  90. a_sync/asyncio/create_task.c +15032 -0
  91. a_sync/asyncio/create_task.cpython-39-darwin.so +0 -0
  92. a_sync/asyncio/create_task.pxd +2 -0
  93. a_sync/asyncio/create_task.pyi +51 -0
  94. a_sync/asyncio/create_task.pyx +268 -0
  95. a_sync/asyncio/gather.c +15735 -0
  96. a_sync/asyncio/gather.cpython-39-darwin.so +0 -0
  97. a_sync/asyncio/gather.pyi +107 -0
  98. a_sync/asyncio/gather.pyx +218 -0
  99. a_sync/asyncio/igather.c +12334 -0
  100. a_sync/asyncio/igather.cpython-39-darwin.so +0 -0
  101. a_sync/asyncio/igather.pxd +1 -0
  102. a_sync/asyncio/igather.pyi +7 -0
  103. a_sync/asyncio/igather.pyx +175 -0
  104. a_sync/asyncio/sleep.c +8916 -0
  105. a_sync/asyncio/sleep.cpython-39-darwin.so +0 -0
  106. a_sync/asyncio/sleep.pyi +14 -0
  107. a_sync/asyncio/sleep.pyx +49 -0
  108. a_sync/debugging.c +15156 -0
  109. a_sync/debugging.cpython-39-darwin.so +0 -0
  110. a_sync/debugging.pyi +73 -0
  111. a_sync/debugging.pyx +107 -0
  112. a_sync/exceptions.c +12952 -0
  113. a_sync/exceptions.cpython-39-darwin.so +0 -0
  114. a_sync/exceptions.pyi +376 -0
  115. a_sync/exceptions.pyx +443 -0
  116. a_sync/executor.py +575 -0
  117. a_sync/functools.c +11489 -0
  118. a_sync/functools.cpython-39-darwin.so +0 -0
  119. a_sync/functools.pxd +7 -0
  120. a_sync/functools.pyi +33 -0
  121. a_sync/functools.pyx +139 -0
  122. a_sync/future.py +1497 -0
  123. a_sync/iter.c +37567 -0
  124. a_sync/iter.cpython-39-darwin.so +0 -0
  125. a_sync/iter.pxd +9 -0
  126. a_sync/iter.pyi +366 -0
  127. a_sync/iter.pyx +981 -0
  128. a_sync/primitives/__init__.pxd +1 -0
  129. a_sync/primitives/__init__.py +53 -0
  130. a_sync/primitives/_debug.c +14737 -0
  131. a_sync/primitives/_debug.cpython-39-darwin.so +0 -0
  132. a_sync/primitives/_debug.pxd +12 -0
  133. a_sync/primitives/_debug.pyi +52 -0
  134. a_sync/primitives/_debug.pyx +223 -0
  135. a_sync/primitives/_loggable.c +10569 -0
  136. a_sync/primitives/_loggable.cpython-39-darwin.so +0 -0
  137. a_sync/primitives/_loggable.pxd +4 -0
  138. a_sync/primitives/_loggable.pyi +66 -0
  139. a_sync/primitives/_loggable.pyx +102 -0
  140. a_sync/primitives/locks/__init__.pxd +8 -0
  141. a_sync/primitives/locks/__init__.py +17 -0
  142. a_sync/primitives/locks/counter.c +16972 -0
  143. a_sync/primitives/locks/counter.cpython-39-darwin.so +0 -0
  144. a_sync/primitives/locks/counter.pxd +12 -0
  145. a_sync/primitives/locks/counter.pyi +151 -0
  146. a_sync/primitives/locks/counter.pyx +260 -0
  147. a_sync/primitives/locks/event.c +16127 -0
  148. a_sync/primitives/locks/event.cpython-39-darwin.so +0 -0
  149. a_sync/primitives/locks/event.pxd +22 -0
  150. a_sync/primitives/locks/event.pyi +43 -0
  151. a_sync/primitives/locks/event.pyx +183 -0
  152. a_sync/primitives/locks/prio_semaphore.c +24084 -0
  153. a_sync/primitives/locks/prio_semaphore.cpython-39-darwin.so +0 -0
  154. a_sync/primitives/locks/prio_semaphore.pxd +24 -0
  155. a_sync/primitives/locks/prio_semaphore.pyi +217 -0
  156. a_sync/primitives/locks/prio_semaphore.pyx +554 -0
  157. a_sync/primitives/locks/semaphore.c +25816 -0
  158. a_sync/primitives/locks/semaphore.cpython-39-darwin.so +0 -0
  159. a_sync/primitives/locks/semaphore.pxd +21 -0
  160. a_sync/primitives/locks/semaphore.pyi +196 -0
  161. a_sync/primitives/locks/semaphore.pyx +452 -0
  162. a_sync/primitives/queue.py +1018 -0
  163. a_sync/py.typed +0 -0
  164. a_sync/sphinx/__init__.py +3 -0
  165. a_sync/sphinx/ext.py +289 -0
  166. a_sync/task.py +916 -0
  167. a_sync/utils/__init__.py +105 -0
  168. a_sync/utils/iterators.py +297 -0
  169. a_sync/utils/repr.c +14354 -0
  170. a_sync/utils/repr.cpython-39-darwin.so +0 -0
  171. a_sync/utils/repr.pyi +2 -0
  172. a_sync/utils/repr.pyx +73 -0
  173. ez_a_sync-0.32.9.dist-info/METADATA +367 -0
  174. ez_a_sync-0.32.9.dist-info/RECORD +177 -0
  175. ez_a_sync-0.32.9.dist-info/WHEEL +6 -0
  176. ez_a_sync-0.32.9.dist-info/licenses/LICENSE.txt +17 -0
  177. ez_a_sync-0.32.9.dist-info/top_level.txt +1 -0
@@ -0,0 +1,21 @@
1
+ from a_sync.primitives._debug cimport _DebugDaemonMixin
2
+
3
+ cdef class Semaphore(_DebugDaemonMixin):
4
+ cdef unsigned long long __value
5
+ cdef object __waiters
6
+ cdef char* _name
7
+ cdef str decode_name(self)
8
+ cdef set _decorated
9
+ cdef dict __dict__
10
+ cpdef bint locked(self)
11
+ cpdef object acquire(self)
12
+ cpdef void release(self)
13
+ cpdef void _wake_up_next(self)
14
+
15
+ cdef class DummySemaphore(Semaphore):
16
+ pass
17
+
18
+ cdef class ThreadsafeSemaphore(Semaphore):
19
+ cdef object semaphores, dummy
20
+ cdef bint use_dummy
21
+ cdef Semaphore c_get_semaphore(self)
@@ -0,0 +1,196 @@
1
+ from a_sync._typing import *
2
+ import asyncio
3
+ import functools
4
+ from _typeshed import Incomplete
5
+ from a_sync.primitives._debug import _DebugDaemonMixin
6
+ from threading import Thread as Thread
7
+
8
+ logger: Incomplete
9
+
10
+ class Semaphore(asyncio.Semaphore, _DebugDaemonMixin):
11
+ """
12
+ A semaphore with additional debugging capabilities inherited from :class:`_DebugDaemonMixin`.
13
+
14
+ This semaphore includes debug logging capabilities that are activated when the semaphore has waiters.
15
+ It allows rewriting the pattern of acquiring a semaphore within a coroutine using a decorator.
16
+
17
+ Example:
18
+ You can write this pattern:
19
+
20
+ ```
21
+ semaphore = Semaphore(5)
22
+
23
+ async def limited():
24
+ async with semaphore:
25
+ return 1
26
+ ```
27
+
28
+ like this:
29
+
30
+ ```
31
+ semaphore = Semaphore(5)
32
+
33
+ @semaphore
34
+ async def limited():
35
+ return 1
36
+ ```
37
+
38
+ See Also:
39
+ :class:`_DebugDaemonMixin` for more details on debugging capabilities.
40
+ """
41
+
42
+ name: Incomplete
43
+ def __init__(self, value: int, name: Incomplete | None = None, **kwargs) -> None:
44
+ """
45
+ Initialize the semaphore with a given value and optional name for debugging.
46
+
47
+ Args:
48
+ value: The initial value for the semaphore.
49
+ name (optional): An optional name used only to provide useful context in debug logs.
50
+ """
51
+
52
+ def __call__(self, fn: CoroFn[P, T]) -> CoroFn[P, T]:
53
+ """
54
+ Decorator method to wrap coroutine functions with the semaphore.
55
+
56
+ This allows rewriting the pattern of acquiring a semaphore within a coroutine using a decorator.
57
+
58
+ Example:
59
+ semaphore = Semaphore(5)
60
+
61
+ @semaphore
62
+ async def limited():
63
+ return 1
64
+ """
65
+
66
+ def __len__(self) -> int: ...
67
+ def decorate(self, fn: CoroFn[P, T]) -> CoroFn[P, T]:
68
+ """
69
+ Wrap a coroutine function to ensure it runs with the semaphore.
70
+
71
+ Example:
72
+ semaphore = Semaphore(5)
73
+
74
+ @semaphore
75
+ async def limited():
76
+ return 1
77
+ """
78
+
79
+ async def acquire(self) -> Literal[True]:
80
+ """
81
+ Acquire the semaphore, ensuring that debug logging is enabled if there are waiters.
82
+
83
+ If the semaphore value is zero or less, the debug daemon is started to log the state of the semaphore.
84
+
85
+ Returns:
86
+ True when the semaphore is successfully acquired.
87
+ """
88
+
89
+ async def _debug_daemon(self) -> None:
90
+ """
91
+ Daemon coroutine (runs in a background task) which will emit a debug log every minute while the semaphore has waiters.
92
+
93
+ This method is part of the :class:`_DebugDaemonMixin` and is used to provide detailed logging information
94
+ about the semaphore's state when it is being waited on.
95
+
96
+ This code will only run if `self.logger.isEnabledFor(logging.DEBUG)` is True. You do not need to include any level checks in your custom implementations.
97
+
98
+ Example:
99
+ semaphore = Semaphore(5)
100
+
101
+ async def monitor():
102
+ await semaphore._debug_daemon()
103
+ """
104
+
105
+ class DummySemaphore(asyncio.Semaphore):
106
+ """
107
+ A dummy semaphore that implements the standard :class:`asyncio.Semaphore` API but does nothing.
108
+
109
+ This class is useful for scenarios where a semaphore interface is required but no actual synchronization is needed.
110
+
111
+ Example:
112
+ dummy_semaphore = DummySemaphore()
113
+
114
+ async def no_op():
115
+ async with dummy_semaphore:
116
+ return 1
117
+ """
118
+
119
+ name: Incomplete
120
+ def __init__(self, name: Optional[str] = None) -> None:
121
+ """
122
+ Initialize the dummy semaphore with an optional name.
123
+
124
+ Args:
125
+ name (optional): An optional name for the dummy semaphore.
126
+ """
127
+
128
+ async def acquire(self) -> Literal[True]:
129
+ """Acquire the dummy semaphore, which is a no-op."""
130
+
131
+ def release(self) -> None:
132
+ """No-op release method."""
133
+
134
+ async def __aenter__(self):
135
+ """No-op context manager entry."""
136
+
137
+ async def __aexit__(self, *args) -> None:
138
+ """No-op context manager exit."""
139
+
140
+ class ThreadsafeSemaphore(Semaphore):
141
+ """
142
+ A semaphore that works in a multi-threaded environment.
143
+
144
+ This semaphore ensures that the program functions correctly even when used with multiple event loops.
145
+ It provides a workaround for edge cases involving multiple threads and event loops by using a separate semaphore
146
+ for each thread.
147
+
148
+ Example:
149
+ semaphore = ThreadsafeSemaphore(5)
150
+
151
+ async def limited():
152
+ async with semaphore:
153
+ return 1
154
+
155
+ See Also:
156
+ :class:`Semaphore` for the base class implementation.
157
+ """
158
+
159
+ semaphores: Incomplete
160
+ dummy: Incomplete
161
+ def __init__(self, value: Optional[int], name: Optional[str] = None) -> None:
162
+ """
163
+ Initialize the threadsafe semaphore with a given value and optional name.
164
+
165
+ Args:
166
+ value: The initial value for the semaphore, should be an integer.
167
+ name (optional): An optional name for the semaphore.
168
+ """
169
+
170
+ def __len__(self) -> int: ...
171
+ @functools.cached_property
172
+ def use_dummy(self) -> bool:
173
+ """
174
+ Determine whether to use a dummy semaphore.
175
+
176
+ Returns:
177
+ True if the semaphore value is None, indicating the use of a dummy semaphore.
178
+ """
179
+
180
+ @property
181
+ def semaphore(self) -> Semaphore:
182
+ """
183
+ Returns the appropriate semaphore for the current thread.
184
+
185
+ NOTE: We can't cache this property because we need to check the current thread every time we access it.
186
+
187
+ Example:
188
+ semaphore = ThreadsafeSemaphore(5)
189
+
190
+ async def limited():
191
+ async with semaphore.semaphore:
192
+ return 1
193
+ """
194
+
195
+ async def __aenter__(self) -> None: ...
196
+ async def __aexit__(self, *args) -> None: ...
@@ -0,0 +1,452 @@
1
+ """
2
+ This module provides various semaphore implementations, including a debug-enabled semaphore,
3
+ a dummy semaphore that does nothing, and a threadsafe semaphore for use in multi-threaded applications.
4
+ """
5
+
6
+ import asyncio
7
+ import collections
8
+ import threading
9
+ from libc.string cimport strcpy
10
+ from libc.stdlib cimport malloc, free
11
+ from typing import Container, Literal, List, Optional, Set
12
+
13
+ from a_sync._typing import CoroFn, P, T
14
+ from a_sync.functools cimport wraps
15
+ from a_sync.primitives._debug cimport _DebugDaemonMixin, _LoopBoundMixin
16
+
17
+
18
+ # cdef asyncio
19
+ cdef object iscoroutinefunction = asyncio.iscoroutinefunction
20
+ cdef object sleep = asyncio.sleep
21
+ cdef object CancelledError = asyncio.CancelledError
22
+ cdef object Future = asyncio.Future
23
+ del asyncio
24
+
25
+ # cdef collections
26
+ cdef object defaultdict = collections.defaultdict
27
+ cdef object deque = collections.deque
28
+ del collections
29
+
30
+ # cdef threading
31
+ cdef object current_thread = threading.current_thread
32
+ cdef object Thread = threading.Thread
33
+ del threading
34
+
35
+
36
+ async def _noop() -> Literal[True]:
37
+ return True
38
+
39
+
40
+ cdef class Semaphore(_DebugDaemonMixin):
41
+ """
42
+ A semaphore with additional debugging capabilities inherited from :class:`_DebugDaemonMixin`.
43
+
44
+ This semaphore includes debug logging capabilities that are activated when the semaphore has waiters.
45
+ It allows rewriting the pattern of acquiring a semaphore within a coroutine using a decorator.
46
+
47
+ Example:
48
+ You can write this pattern:
49
+
50
+ ```
51
+ semaphore = Semaphore(5)
52
+
53
+ async def limited():
54
+ async with semaphore:
55
+ return 1
56
+ ```
57
+
58
+ like this:
59
+
60
+ ```
61
+ semaphore = Semaphore(5)
62
+
63
+ @semaphore
64
+ async def limited():
65
+ return 1
66
+ ```
67
+
68
+ See Also:
69
+ :class:`_DebugDaemonMixin` for more details on debugging capabilities.
70
+ """
71
+
72
+ def __cinit__(self) -> None:
73
+ self._Semaphore__waiters = deque()
74
+ self._decorated: Set[str] = set()
75
+
76
+ def __init__(
77
+ self,
78
+ value: int = 1,
79
+ str name = "",
80
+ object loop = None,
81
+ ) -> None:
82
+ """
83
+ Initialize the semaphore with a given value and optional name for debugging.
84
+
85
+ Args:
86
+ value: The initial value for the semaphore.
87
+ name (optional): An optional name used only to provide useful context in debug logs.
88
+ """
89
+ _LoopBoundMixin.__init__(self, loop=loop)
90
+ if value < 0:
91
+ raise ValueError("Semaphore initial value must be >= 0")
92
+
93
+ try:
94
+ self._Semaphore__value = value
95
+ except OverflowError as e:
96
+ if value < 0:
97
+ raise ValueError("'value' must be a positive integer") from e.__cause__
98
+ raise OverflowError(
99
+ {"error": str(e), "value": value, "max value": 18446744073709551615},
100
+ "If you need a Semaphore with a larger value, you should just use asyncio.Semaphore",
101
+ ) from e.__cause__
102
+
103
+ # we need a constant to coerce to char*
104
+ cdef bytes encoded_name
105
+ try:
106
+ encoded_name = (name or getattr(self, "__origin__", "")).encode("utf-8")
107
+ except AttributeError:
108
+ # just a failsafe, please use string inputs
109
+ encoded_name = repr(name).encode("utf-8")
110
+
111
+ # Allocate memory for the char* and add 1 for the null character
112
+ cdef Py_ssize_t length = len(encoded_name)
113
+ self._name = <char*>malloc(length + 1)
114
+ """An optional name for the counter, used in debug logs."""
115
+
116
+ if self._name == NULL:
117
+ raise MemoryError("Failed to allocate memory for __name.")
118
+ # Copy the bytes data into the char*
119
+ strcpy(self._name, encoded_name)
120
+
121
+ def __dealloc__(self):
122
+ # Free the memory allocated for _name
123
+ if self._name is not NULL:
124
+ free(self._name)
125
+
126
+ def __call__(self, fn: CoroFn[P, T]) -> CoroFn[P, T]:
127
+ """
128
+ Decorator method to wrap coroutine functions with the semaphore.
129
+
130
+ This allows rewriting the pattern of acquiring a semaphore within a coroutine using a decorator.
131
+
132
+ Example:
133
+ semaphore = Semaphore(5)
134
+
135
+ @semaphore
136
+ async def limited():
137
+ return 1
138
+ """
139
+ return self.decorate(fn) # type: ignore [arg-type, return-value]
140
+
141
+ def __repr__(self) -> str:
142
+ representation = f"<{self.__class__.__name__} name={self.decode_name()} value={self._Semaphore__value} waiters={len(self)}>"
143
+ if self._decorated:
144
+ detail = next(iter(decorated)) if len(decorated := self._decorated) == 1 else decorated
145
+ representation = f"{representation[:-1]} decorates={detail}"
146
+ return representation
147
+
148
+ async def __aenter__(self):
149
+ await self.acquire()
150
+ # We have no use for the "as ..." clause in the with
151
+ # statement for locks.
152
+ return None
153
+
154
+ async def __aexit__(self, exc_type, exc, tb):
155
+ self.release()
156
+
157
+ cpdef bint locked(self):
158
+ """Returns True if semaphore cannot be acquired immediately."""
159
+ if self._Semaphore__value == 0:
160
+ return True
161
+ for w in self._Semaphore__waiters:
162
+ if _is_not_cancelled(w):
163
+ return True
164
+
165
+ def __len__(self) -> int:
166
+ return len(self._Semaphore__waiters)
167
+
168
+ def decorate(self, fn: CoroFn[P, T]) -> CoroFn[P, T]:
169
+ """
170
+ Wrap a coroutine function to ensure it runs with the semaphore.
171
+
172
+ Example:
173
+ semaphore = Semaphore(5)
174
+
175
+ @semaphore
176
+ async def limited():
177
+ return 1
178
+ """
179
+ if not iscoroutinefunction(fn):
180
+ raise TypeError(f"{fn} must be a coroutine function")
181
+
182
+ @wraps(fn)
183
+ async def semaphore_wrapper(*args: P.args, **kwargs: P.kwargs) -> T:
184
+ async with self:
185
+ return await fn(*args, **kwargs)
186
+
187
+ self._decorated.add(f"{fn.__module__}.{fn.__name__}")
188
+ return semaphore_wrapper
189
+
190
+ cpdef object acquire(self):
191
+ """
192
+ Acquire the semaphore, ensuring that debug logging is enabled if there are waiters.
193
+
194
+ If the internal counter is larger than zero on entry, decrement it by one and return
195
+ True immediately. If it is zero on entry, block, waiting until some other coroutine
196
+ has called release() to make it larger than 0, and then return True.
197
+
198
+ If the semaphore value is zero or less, the debug daemon is started to log the state of the semaphore.
199
+
200
+ Returns:
201
+ True when the semaphore is successfully acquired.
202
+ """
203
+ if self._Semaphore__value <= 0:
204
+ self._c_ensure_debug_daemon((),{})
205
+
206
+ if not self.locked():
207
+ self._Semaphore__value -= 1
208
+ return _noop()
209
+
210
+ return self.__acquire()
211
+
212
+ async def __acquire(self) -> Literal[True]:
213
+ # Finally block should be called before the CancelledError
214
+ # handling as we don't want CancelledError to call
215
+ # _wake_up_first() and attempt to wake up itself.
216
+
217
+ cdef object fut = self._c_get_loop().create_future()
218
+ self._Semaphore__waiters.append(fut)
219
+ try:
220
+ try:
221
+ await fut
222
+ finally:
223
+ self._Semaphore__waiters.remove(fut)
224
+ except CancelledError:
225
+ if _is_not_cancelled(fut):
226
+ self._Semaphore__value += 1
227
+ self._wake_up_next()
228
+ raise
229
+
230
+ if self._Semaphore__value > 0:
231
+ self._wake_up_next()
232
+
233
+ return True
234
+
235
+ cpdef void release(self):
236
+ """Release a semaphore, incrementing the internal counter by one.
237
+
238
+ When it was zero on entry and another coroutine is waiting for it to
239
+ become larger than zero again, wake up that coroutine.
240
+ """
241
+ self._Semaphore__value += 1
242
+ self._wake_up_next()
243
+
244
+ @property
245
+ def name(self) -> str:
246
+ return self.decode_name()
247
+
248
+ cdef str decode_name(self):
249
+ return (self._name or b"").decode("utf-8")
250
+
251
+ @property
252
+ def _value(self) -> int:
253
+ # required for subclass compatability
254
+ return self._Semaphore__value
255
+
256
+ @_value.setter
257
+ def _value(self, unsigned long long value):
258
+ # required for subclass compatability
259
+ self._Semaphore__value = value
260
+
261
+ @property
262
+ def _waiters(self) -> List[Future]:
263
+ # required for subclass compatability
264
+ return self._Semaphore__waiters
265
+
266
+ @_waiters.setter
267
+ def _waiters(self, value: Container):
268
+ # required for subclass compatability
269
+ self._Semaphore__waiters = value
270
+
271
+ cpdef void _wake_up_next(self):
272
+ """Wake up the first waiter that isn't done."""
273
+ if not self._Semaphore__waiters:
274
+ return
275
+
276
+ for fut in self._Semaphore__waiters:
277
+ if _is_not_done(fut):
278
+ self._Semaphore__value -= 1
279
+ fut.set_result(True)
280
+ return
281
+
282
+ async def _debug_daemon(self) -> None:
283
+ """
284
+ Daemon coroutine (runs in a background task) which will emit a debug log every minute while the semaphore has waiters.
285
+
286
+ This method is part of the :class:`_DebugDaemonMixin` and is used to provide detailed logging information
287
+ about the semaphore's state when it is being waited on.
288
+
289
+ This code will only run if `self.logger.isEnabledFor(logging.DEBUG)` is True. You do not need to include any level checks in your custom implementations.
290
+
291
+ Example:
292
+ semaphore = Semaphore(5)
293
+
294
+ async def monitor():
295
+ await semaphore._debug_daemon()
296
+ """
297
+ cdef object waiters = self._Semaphore__waiters
298
+ cdef set decorated = self._decorated
299
+ cdef object log = self.get_logger().debug
300
+ while waiters:
301
+ await sleep(60)
302
+ if waiters:
303
+ len_decorated = len(decorated)
304
+ if len_decorated == 0:
305
+ log("%s has %s waiters", self, len(self))
306
+ elif len_decorated == 1:
307
+ log(
308
+ "%s has %s waiters for %s",
309
+ self,
310
+ len(self),
311
+ next(iter(decorated)),
312
+ )
313
+ else:
314
+ log(
315
+ "%s has %s waiters for any of: %s",
316
+ self,
317
+ len(self),
318
+ decorated,
319
+ )
320
+
321
+
322
+ cdef inline bint _is_not_done(fut: Future):
323
+ return <str>fut._state == "PENDING"
324
+
325
+ cdef inline bint _is_not_cancelled(fut: Future):
326
+ return <str>fut._state != "CANCELLED"
327
+
328
+
329
+ cdef class DummySemaphore(Semaphore):
330
+ """
331
+ A dummy semaphore that implements the standard :class:`asyncio.Semaphore` API but does nothing.
332
+
333
+ This class is useful for scenarios where a semaphore interface is required but no actual synchronization is needed.
334
+
335
+ Example:
336
+ dummy_semaphore = DummySemaphore()
337
+
338
+ async def no_op():
339
+ async with dummy_semaphore:
340
+ return 1
341
+ """
342
+
343
+ def __cinit__(self):
344
+ self._Semaphore__value = 0
345
+ self._Semaphore__waiters = deque()
346
+ self._decorated = None
347
+
348
+ def __init__(self, name: Optional[str] = None):
349
+ """
350
+ Initialize the dummy semaphore with an optional name.
351
+
352
+ Args:
353
+ name (optional): An optional name for the dummy semaphore.
354
+ """
355
+
356
+ # we need a constant to coerce to char*
357
+ cdef bytes encoded_name = (name or getattr(self, "__origin__", "")).encode("utf-8")
358
+ cdef Py_ssize_t length = len(encoded_name)
359
+
360
+ # Allocate memory for the char* and add 1 for the null character
361
+ self._name = <char*>malloc(length + 1)
362
+ """An optional name for the counter, used in debug logs."""
363
+
364
+ if self._name == NULL:
365
+ raise MemoryError("Failed to allocate memory for _name.")
366
+
367
+ # Copy the bytes data into the char*
368
+ strcpy(self._name, encoded_name)
369
+
370
+ def __repr__(self) -> str:
371
+ return "<{} name={}>".format(self.__class__.__name__, self.decode_name())
372
+
373
+ async def acquire(self) -> Literal[True]:
374
+ """Acquire the dummy semaphore, which is a no-op."""
375
+ return True
376
+
377
+ cpdef void release(self):
378
+ """No-op release method."""
379
+
380
+ async def __aenter__(self):
381
+ """No-op context manager entry."""
382
+ return self
383
+
384
+ async def __aexit__(self, *args):
385
+ """No-op context manager exit."""
386
+
387
+
388
+ cdef class ThreadsafeSemaphore(Semaphore):
389
+ """
390
+ A semaphore that works in a multi-threaded environment.
391
+
392
+ This semaphore ensures that the program functions correctly even when used with multiple event loops.
393
+ It provides a workaround for edge cases involving multiple threads and event loops by using a separate semaphore
394
+ for each thread.
395
+
396
+ Example:
397
+ semaphore = ThreadsafeSemaphore(5)
398
+
399
+ async def limited():
400
+ async with semaphore:
401
+ return 1
402
+
403
+ See Also:
404
+ :class:`Semaphore` for the base class implementation.
405
+ """
406
+
407
+ def __init__(self, value: Optional[int], name: Optional[str] = None) -> None:
408
+ """
409
+ Initialize the threadsafe semaphore with a given value and optional name.
410
+
411
+ Args:
412
+ value: The initial value for the semaphore, should be an integer.
413
+ name (optional): An optional name for the semaphore.
414
+ """
415
+ assert isinstance(value, int), f"{value} should be an integer."
416
+ Semaphore.__init__(self, value, name=name)
417
+
418
+ self.use_dummy = value is -1
419
+ if self.use_dummy:
420
+ self.semaphores = {}
421
+ self.dummy = DummySemaphore(name=name)
422
+ else:
423
+ self.semaphores: DefaultDict[Thread, Semaphore] = defaultdict(lambda: Semaphore(value, name=name)) # type: ignore [arg-type]
424
+ self.dummy = None
425
+
426
+ def __len__(self) -> int:
427
+ cdef dict[object, Semaphore] semaphores = self.semaphores
428
+ return sum(map(len, semaphores.values()))
429
+
430
+ @property
431
+ def semaphore(self) -> Semaphore:
432
+ """
433
+ Returns the appropriate semaphore for the current thread.
434
+
435
+ NOTE: We can't cache this property because we need to check the current thread every time we access it.
436
+
437
+ Example:
438
+ semaphore = ThreadsafeSemaphore(5)
439
+
440
+ async def limited():
441
+ async with semaphore.semaphore:
442
+ return 1
443
+ """
444
+
445
+ cdef Semaphore c_get_semaphore(self):
446
+ return self.dummy if self.use_dummy else self.semaphores[current_thread()]
447
+
448
+ async def __aenter__(self):
449
+ await self.c_get_semaphore().acquire()
450
+
451
+ async def __aexit__(self, *args):
452
+ self.c_get_semaphore().release()