anyio-executor 0.1.0__py3-none-any.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.
- anyio_executor/__init__.py +6 -0
- anyio_executor/azip.py +111 -0
- anyio_executor/core.py +412 -0
- anyio_executor/py.typed +0 -0
- anyio_executor-0.1.0.dist-info/METADATA +45 -0
- anyio_executor-0.1.0.dist-info/RECORD +9 -0
- anyio_executor-0.1.0.dist-info/WHEEL +5 -0
- anyio_executor-0.1.0.dist-info/licenses/LICENSE +201 -0
- anyio_executor-0.1.0.dist-info/top_level.txt +1 -0
anyio_executor/azip.py
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import sys
|
|
2
|
+
from collections.abc import AsyncIterable, AsyncIterator
|
|
3
|
+
from typing import Any, Generic, TypeVar, overload
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
if sys.version_info < (3, 11):
|
|
7
|
+
from typing_extensions import Self
|
|
8
|
+
else:
|
|
9
|
+
from typing import Self
|
|
10
|
+
|
|
11
|
+
# There are other asynchronous azip libraries out there
|
|
12
|
+
# but the maitnence of those can be questioned so here's
|
|
13
|
+
# our own version
|
|
14
|
+
T = TypeVar("T")
|
|
15
|
+
T1 = TypeVar("T1")
|
|
16
|
+
T2 = TypeVar("T2")
|
|
17
|
+
T3 = TypeVar("T3")
|
|
18
|
+
T4 = TypeVar("T4")
|
|
19
|
+
T5 = TypeVar("T5")
|
|
20
|
+
|
|
21
|
+
_T_co = TypeVar("_T_co", covariant=True)
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
# NOTE: AsyncIterator has an __anext__ function taken care of for us so it's not here...
|
|
25
|
+
class azip(AsyncIterator[_T_co]):
|
|
26
|
+
__slots__ = ("_iters", "_strict", "_tuplesize")
|
|
27
|
+
_ites: tuple[AsyncIterable[Any], ...]
|
|
28
|
+
_strict: bool
|
|
29
|
+
_tuplesize: int
|
|
30
|
+
|
|
31
|
+
@overload
|
|
32
|
+
def __init__(self: "azip[Any]") -> None: ...
|
|
33
|
+
@overload
|
|
34
|
+
def __init__(self: "azip[tuple[T1]]", iter1: AsyncIterable[T1], /): ...
|
|
35
|
+
@overload
|
|
36
|
+
def __init__(
|
|
37
|
+
self: "azip[tuple[T1, T2]]",
|
|
38
|
+
iter1: AsyncIterable[T1],
|
|
39
|
+
iter2: AsyncIterable[T2],
|
|
40
|
+
/,
|
|
41
|
+
) -> None: ...
|
|
42
|
+
@overload
|
|
43
|
+
def __init__(
|
|
44
|
+
self: "azip[tuple[T1, T2, T3]]",
|
|
45
|
+
iter1: AsyncIterable[T1],
|
|
46
|
+
iter2: AsyncIterable[T2],
|
|
47
|
+
iter3: AsyncIterable[T3],
|
|
48
|
+
/,
|
|
49
|
+
) -> None: ...
|
|
50
|
+
@overload
|
|
51
|
+
def __init__(
|
|
52
|
+
self: "azip[tuple[T1, T2, T3, T4]]",
|
|
53
|
+
iter1: AsyncIterable[T1],
|
|
54
|
+
iter2: AsyncIterable[T2],
|
|
55
|
+
iter3: AsyncIterable[T3],
|
|
56
|
+
iter4: AsyncIterable[T4],
|
|
57
|
+
/,
|
|
58
|
+
*,
|
|
59
|
+
strict: bool = False,
|
|
60
|
+
): ...
|
|
61
|
+
@overload
|
|
62
|
+
def __init__(
|
|
63
|
+
self: "azip[tuple[T1, T2, T3, T4, T5]]",
|
|
64
|
+
iter1: AsyncIterable[T1],
|
|
65
|
+
iter2: AsyncIterable[T2],
|
|
66
|
+
iter3: AsyncIterable[T3],
|
|
67
|
+
iter4: AsyncIterable[T4],
|
|
68
|
+
iter5: AsyncIterable[T5],
|
|
69
|
+
/,
|
|
70
|
+
*,
|
|
71
|
+
strict: bool = False,
|
|
72
|
+
): ...
|
|
73
|
+
|
|
74
|
+
def __init__(
|
|
75
|
+
self, *iters: AsyncIterable[Any], strict: bool = False
|
|
76
|
+
) -> "azip[tuple[Any, ...]]":
|
|
77
|
+
self._iters = tuple(map(aiter, iters))
|
|
78
|
+
self._strict = strict
|
|
79
|
+
self._tuplesize = len(self._iters)
|
|
80
|
+
|
|
81
|
+
async def __anext__(self) -> _T_co:
|
|
82
|
+
# "i" needs to be kept in scope.
|
|
83
|
+
i = 0
|
|
84
|
+
try:
|
|
85
|
+
results = []
|
|
86
|
+
for i in range(self._tuplesize):
|
|
87
|
+
results.append(await anext(self._iters[i]))
|
|
88
|
+
return tuple(results)
|
|
89
|
+
except StopAsyncIteration as err:
|
|
90
|
+
if self._strict:
|
|
91
|
+
# The diagnosis part can be the trickiest portion
|
|
92
|
+
# Not even zip iteself. A Rough version of CPython's
|
|
93
|
+
# C Version was enough to logically get the job
|
|
94
|
+
# completed.
|
|
95
|
+
|
|
96
|
+
if i:
|
|
97
|
+
plural = " " if i == 1 else "s 1-"
|
|
98
|
+
raise ValueError(
|
|
99
|
+
f"azip() argument {i + 1} is shorter than argument{plural}{i}"
|
|
100
|
+
)
|
|
101
|
+
for i in range(1, self._tuplesize):
|
|
102
|
+
try:
|
|
103
|
+
await anext(self._iters[i])
|
|
104
|
+
plural = " " if i == 1 else "s 1-"
|
|
105
|
+
raise ValueError(
|
|
106
|
+
f"azip() argument {i + 1} is longer"
|
|
107
|
+
f" than argument{plural}{i}"
|
|
108
|
+
)
|
|
109
|
+
except StopAsyncIteration:
|
|
110
|
+
continue
|
|
111
|
+
raise err
|
anyio_executor/core.py
ADDED
|
@@ -0,0 +1,412 @@
|
|
|
1
|
+
import sys
|
|
2
|
+
from collections.abc import (
|
|
3
|
+
AsyncIterable,
|
|
4
|
+
AsyncIterator,
|
|
5
|
+
Awaitable,
|
|
6
|
+
Callable,
|
|
7
|
+
Coroutine,
|
|
8
|
+
Generator,
|
|
9
|
+
Iterable,
|
|
10
|
+
Iterator,
|
|
11
|
+
Sequence,
|
|
12
|
+
)
|
|
13
|
+
from contextlib import contextmanager
|
|
14
|
+
from dataclasses import dataclass
|
|
15
|
+
from functools import partial
|
|
16
|
+
from types import TracebackType
|
|
17
|
+
from typing import Any, Generic, TypeVar, final, overload
|
|
18
|
+
|
|
19
|
+
import anyio
|
|
20
|
+
from aiologic import CountdownEvent, SimpleQueue
|
|
21
|
+
|
|
22
|
+
from .azip import azip
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
if sys.version_info < (3, 11):
|
|
26
|
+
from typing_extensions import Self
|
|
27
|
+
else:
|
|
28
|
+
from typing import Self
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
R = TypeVar("R")
|
|
32
|
+
T = TypeVar("T")
|
|
33
|
+
T1 = TypeVar("T1")
|
|
34
|
+
T2 = TypeVar("T2")
|
|
35
|
+
T3 = TypeVar("T3")
|
|
36
|
+
T4 = TypeVar("T4")
|
|
37
|
+
T5 = TypeVar("T5")
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
@dataclass(slots=True)
|
|
41
|
+
class _WorkItem(Generic[T]):
|
|
42
|
+
func: Callable[[], Awaitable[T]]
|
|
43
|
+
exc_handler: Callable[[BaseException], None] | None
|
|
44
|
+
queue: SimpleQueue[T]
|
|
45
|
+
|
|
46
|
+
async def execute(self) -> None:
|
|
47
|
+
try:
|
|
48
|
+
await self.queue.async_put(await self.func())
|
|
49
|
+
except Exception as e:
|
|
50
|
+
if self.exc_handler:
|
|
51
|
+
self.exc_handler(e)
|
|
52
|
+
else:
|
|
53
|
+
raise e
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
@dataclass(slots=True)
|
|
57
|
+
class ExecutorResult(Awaitable[list[T]], AsyncIterable[T]):
|
|
58
|
+
"""used for waiting upon results to return in different asynchronous
|
|
59
|
+
fashions."""
|
|
60
|
+
|
|
61
|
+
queue: SimpleQueue[T]
|
|
62
|
+
count: CountdownEvent
|
|
63
|
+
executor: "Executor"
|
|
64
|
+
|
|
65
|
+
async def __aiter__(self) -> AsyncIterator[T]:
|
|
66
|
+
while not self.executor.is_closed:
|
|
67
|
+
item = await self.queue.async_get()
|
|
68
|
+
yield item
|
|
69
|
+
|
|
70
|
+
self.count.down()
|
|
71
|
+
if bool(self.count):
|
|
72
|
+
break
|
|
73
|
+
|
|
74
|
+
async def __wait__(self) -> list[T]:
|
|
75
|
+
return [i async for i in self]
|
|
76
|
+
|
|
77
|
+
def __await__(self) -> Generator[Any, None, list[T]]:
|
|
78
|
+
return self.__wait__().__await__()
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
# inspired by aiolibs_executor.
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
@final
|
|
85
|
+
class Executor:
|
|
86
|
+
"""A small, yet effective and efficient asynchronous executor object."""
|
|
87
|
+
|
|
88
|
+
__slots__ = (
|
|
89
|
+
"__weakref__",
|
|
90
|
+
"_num_workers",
|
|
91
|
+
"_work_queue",
|
|
92
|
+
"_exc_handler",
|
|
93
|
+
"_tg",
|
|
94
|
+
"_workers_done",
|
|
95
|
+
)
|
|
96
|
+
|
|
97
|
+
def __init__(
|
|
98
|
+
self,
|
|
99
|
+
num_workers: int = 0,
|
|
100
|
+
exc_handler: Callable[[BaseException], None] | None = None,
|
|
101
|
+
) -> None:
|
|
102
|
+
"""
|
|
103
|
+
:param num_workers:
|
|
104
|
+
A number of worker tasks to provide defaults to 16.
|
|
105
|
+
:param exc_handler:
|
|
106
|
+
An optional exception handler to call if a function
|
|
107
|
+
executed or submitted fails.
|
|
108
|
+
"""
|
|
109
|
+
|
|
110
|
+
if num_workers < 0:
|
|
111
|
+
raise ValueError("num_wokers must be a positive integer")
|
|
112
|
+
if num_workers == 0:
|
|
113
|
+
num_workers = 16
|
|
114
|
+
|
|
115
|
+
self._num_workers = num_workers
|
|
116
|
+
self._work_queue: SimpleQueue[_WorkItem[T] | None] = SimpleQueue()
|
|
117
|
+
self._exc_handler = exc_handler
|
|
118
|
+
self._tg = anyio.create_task_group()
|
|
119
|
+
self._workers_done = CountdownEvent()
|
|
120
|
+
|
|
121
|
+
@property
|
|
122
|
+
def is_closed(self) -> bool:
|
|
123
|
+
"""checks if executor is not currently running"""
|
|
124
|
+
return bool(self._workers_done)
|
|
125
|
+
|
|
126
|
+
async def _start(self) -> None:
|
|
127
|
+
for _ in range(self._num_workers):
|
|
128
|
+
await self._tg.start(self._work)
|
|
129
|
+
|
|
130
|
+
async def __aenter__(self) -> Self:
|
|
131
|
+
await self._tg.__aenter__()
|
|
132
|
+
await self._start()
|
|
133
|
+
return self
|
|
134
|
+
|
|
135
|
+
@contextmanager
|
|
136
|
+
def _worker_scope(self) -> Iterator[None]:
|
|
137
|
+
"""Assistance with Lazy event up/down event functionality."""
|
|
138
|
+
self._workers_done.up()
|
|
139
|
+
try:
|
|
140
|
+
yield
|
|
141
|
+
finally:
|
|
142
|
+
self._workers_done.down()
|
|
143
|
+
|
|
144
|
+
async def _work(self, task_status=anyio.TASK_STATUS_IGNORED) -> None:
|
|
145
|
+
task_status.started()
|
|
146
|
+
with self._worker_scope():
|
|
147
|
+
while item := await self._work_queue.async_get():
|
|
148
|
+
await item.execute()
|
|
149
|
+
|
|
150
|
+
# seperate tasks used for task queing to
|
|
151
|
+
# allow for simultaneious memory loading
|
|
152
|
+
# without eating up too much iterators
|
|
153
|
+
# will immediately run alongside which
|
|
154
|
+
# is what we want.
|
|
155
|
+
async def _load_tasks_sync_iter(
|
|
156
|
+
self,
|
|
157
|
+
queue: SimpleQueue[T],
|
|
158
|
+
counter: CountdownEvent,
|
|
159
|
+
func: Callable[..., Awaitable[R] | Coroutine[Any, Any, R]],
|
|
160
|
+
exc_handler: Callable[[BaseException], None] | None,
|
|
161
|
+
it: Iterable[Any],
|
|
162
|
+
/,
|
|
163
|
+
*its: Iterable[Any],
|
|
164
|
+
) -> None:
|
|
165
|
+
for args in zip(it, *its, strict=False):
|
|
166
|
+
counter.up()
|
|
167
|
+
await self._work_queue.async_put(
|
|
168
|
+
_WorkItem(partial(func, *args), exc_handler, queue)
|
|
169
|
+
)
|
|
170
|
+
# we used a single up to ensure ExecutorResult
|
|
171
|
+
# can't be shut down before results come in.
|
|
172
|
+
counter.down()
|
|
173
|
+
|
|
174
|
+
async def _load_tasks_async_iter(
|
|
175
|
+
self,
|
|
176
|
+
queue: SimpleQueue[T],
|
|
177
|
+
counter: CountdownEvent,
|
|
178
|
+
func: Callable[..., Awaitable[R] | Coroutine[Any, Any, R]],
|
|
179
|
+
exc_handler: Callable[[BaseException], None] | None,
|
|
180
|
+
it: AsyncIterable[Any],
|
|
181
|
+
/,
|
|
182
|
+
*its: AsyncIterable[Any],
|
|
183
|
+
) -> None:
|
|
184
|
+
# A Custom azip object was made just for asynchronous packing.
|
|
185
|
+
# It even has an unused feature for diagnosing using strict
|
|
186
|
+
# (remains unused).
|
|
187
|
+
|
|
188
|
+
async for args in azip(it, *its, strict=False):
|
|
189
|
+
counter.up()
|
|
190
|
+
await self._work_queue.async_put(
|
|
191
|
+
_WorkItem(partial(func, *args), exc_handler, queue)
|
|
192
|
+
)
|
|
193
|
+
counter.down()
|
|
194
|
+
|
|
195
|
+
@overload
|
|
196
|
+
def map(
|
|
197
|
+
self,
|
|
198
|
+
func: Callable[[T1], Awaitable[R] | Coroutine[Any, Any, R]],
|
|
199
|
+
it1: Iterable[T1] | Sequence[T1],
|
|
200
|
+
/,
|
|
201
|
+
*,
|
|
202
|
+
exc_handler: Callable[[BaseException], None] | None = None,
|
|
203
|
+
) -> ExecutorResult[R]: ...
|
|
204
|
+
|
|
205
|
+
@overload
|
|
206
|
+
def map(
|
|
207
|
+
self,
|
|
208
|
+
func: Callable[[T1, T2], Awaitable[R] | Coroutine[Any, Any, R]],
|
|
209
|
+
it1: Iterable[T1] | Sequence[T1],
|
|
210
|
+
it2: Iterable[T2] | Sequence[T2],
|
|
211
|
+
/,
|
|
212
|
+
*,
|
|
213
|
+
exc_handler: Callable[[BaseException], None] | None = None,
|
|
214
|
+
) -> ExecutorResult[R]: ...
|
|
215
|
+
|
|
216
|
+
@overload
|
|
217
|
+
def map(
|
|
218
|
+
self,
|
|
219
|
+
func: Callable[[T1, T2, T3], Awaitable[R] | Coroutine[Any, Any, R]],
|
|
220
|
+
it1: Iterable[T1] | Sequence[T1],
|
|
221
|
+
it2: Iterable[T2] | Sequence[T2],
|
|
222
|
+
it3: Iterable[T3] | Sequence[T3],
|
|
223
|
+
/,
|
|
224
|
+
*,
|
|
225
|
+
exc_handler: Callable[[BaseException], None] | None = None,
|
|
226
|
+
) -> ExecutorResult[R]: ...
|
|
227
|
+
|
|
228
|
+
@overload
|
|
229
|
+
def map(
|
|
230
|
+
self,
|
|
231
|
+
func: Callable[[T1, T2, T3, T4], Awaitable[R] | Coroutine[Any, Any, R]],
|
|
232
|
+
it1: Iterable[T1] | Sequence[T1],
|
|
233
|
+
it2: Iterable[T2] | Sequence[T2],
|
|
234
|
+
it3: Iterable[T3] | Sequence[T3],
|
|
235
|
+
it4: Iterable[T4] | Sequence[T4],
|
|
236
|
+
/,
|
|
237
|
+
*,
|
|
238
|
+
exc_handler: Callable[[BaseException], None] | None = None,
|
|
239
|
+
) -> ExecutorResult[R]: ...
|
|
240
|
+
|
|
241
|
+
@overload
|
|
242
|
+
def map(
|
|
243
|
+
self,
|
|
244
|
+
func: Callable[[T1, T2, T3, T4, T5], Awaitable[R] | Coroutine[Any, Any, R]],
|
|
245
|
+
it1: Iterable[T1] | Sequence[T1],
|
|
246
|
+
it2: Iterable[T2] | Sequence[T2],
|
|
247
|
+
it3: Iterable[T3] | Sequence[T3],
|
|
248
|
+
it4: Iterable[T4] | Sequence[T4],
|
|
249
|
+
it5: Iterable[T5] | Sequence[T5],
|
|
250
|
+
/,
|
|
251
|
+
*,
|
|
252
|
+
exc_handler: Callable[[BaseException], None] | None = None,
|
|
253
|
+
) -> ExecutorResult[R]: ...
|
|
254
|
+
|
|
255
|
+
def map(
|
|
256
|
+
self,
|
|
257
|
+
func: Callable[..., Awaitable[R] | Coroutine[Any, Any, R]],
|
|
258
|
+
it: Iterable[Any] | Sequence[Any],
|
|
259
|
+
/,
|
|
260
|
+
*its: Iterable[Any] | Sequence[Any],
|
|
261
|
+
exc_handler: Callable[[BaseException], None] | None = None,
|
|
262
|
+
) -> ExecutorResult[R]:
|
|
263
|
+
"""Executes an asynchronous function concurrently with
|
|
264
|
+
synchronous iterables to use.
|
|
265
|
+
|
|
266
|
+
:param func: The function to run.
|
|
267
|
+
:param exc_handler: Provides an alternative solution
|
|
268
|
+
for handling given exceptions, if not selected
|
|
269
|
+
for this function will back off to whatever
|
|
270
|
+
the executor class has provided and if that
|
|
271
|
+
is not the case then the exception will
|
|
272
|
+
be raised.
|
|
273
|
+
|
|
274
|
+
:raise RuntimeError:
|
|
275
|
+
If executor has been previously shut down.
|
|
276
|
+
"""
|
|
277
|
+
if self.is_closed:
|
|
278
|
+
raise RuntimeError("Executor already closed.")
|
|
279
|
+
cd = CountdownEvent()
|
|
280
|
+
cd.up()
|
|
281
|
+
queue: SimpleQueue[T] = SimpleQueue()
|
|
282
|
+
self._tg.start_soon(
|
|
283
|
+
self._load_tasks_sync_iter,
|
|
284
|
+
queue,
|
|
285
|
+
cd,
|
|
286
|
+
func,
|
|
287
|
+
exc_handler or self._exc_handler,
|
|
288
|
+
it,
|
|
289
|
+
*its,
|
|
290
|
+
)
|
|
291
|
+
return ExecutorResult(queue, cd, self)
|
|
292
|
+
|
|
293
|
+
@overload
|
|
294
|
+
def amap(
|
|
295
|
+
self,
|
|
296
|
+
func: Callable[[T1], Awaitable[R] | Coroutine[Any, Any, R]],
|
|
297
|
+
it1: AsyncIterable[T1],
|
|
298
|
+
/,
|
|
299
|
+
*,
|
|
300
|
+
exc_handler: Callable[[BaseException], None] | None = None,
|
|
301
|
+
) -> ExecutorResult[R]: ...
|
|
302
|
+
|
|
303
|
+
@overload
|
|
304
|
+
def amap(
|
|
305
|
+
self,
|
|
306
|
+
func: Callable[[T1, T2], Awaitable[R] | Coroutine[Any, Any, R]],
|
|
307
|
+
it1: AsyncIterable[T1],
|
|
308
|
+
it2: AsyncIterable[T2],
|
|
309
|
+
/,
|
|
310
|
+
*,
|
|
311
|
+
exc_handler: Callable[[BaseException], None] | None = None,
|
|
312
|
+
) -> ExecutorResult[R]: ...
|
|
313
|
+
|
|
314
|
+
@overload
|
|
315
|
+
def amap(
|
|
316
|
+
self,
|
|
317
|
+
func: Callable[[T1, T2, T3], Awaitable[R] | Coroutine[Any, Any, R]],
|
|
318
|
+
it1: AsyncIterable[T1],
|
|
319
|
+
it2: AsyncIterable[T2],
|
|
320
|
+
it3: AsyncIterable[T3],
|
|
321
|
+
/,
|
|
322
|
+
*,
|
|
323
|
+
exc_handler: Callable[[BaseException], None] | None = None,
|
|
324
|
+
) -> ExecutorResult[R]: ...
|
|
325
|
+
|
|
326
|
+
@overload
|
|
327
|
+
def amap(
|
|
328
|
+
self,
|
|
329
|
+
func: Callable[[T1, T2, T3, T4], Awaitable[R] | Coroutine[Any, Any, R]],
|
|
330
|
+
it1: AsyncIterable[T1],
|
|
331
|
+
it2: AsyncIterable[T2],
|
|
332
|
+
it3: AsyncIterable[T3],
|
|
333
|
+
it4: AsyncIterable[T4],
|
|
334
|
+
/,
|
|
335
|
+
*,
|
|
336
|
+
exc_handler: Callable[[BaseException], None] | None = None,
|
|
337
|
+
) -> ExecutorResult[R]: ...
|
|
338
|
+
|
|
339
|
+
@overload
|
|
340
|
+
def amap(
|
|
341
|
+
self,
|
|
342
|
+
func: Callable[[T1, T2, T3, T4, T5], Awaitable[R] | Coroutine[Any, Any, R]],
|
|
343
|
+
it1: AsyncIterable[T1],
|
|
344
|
+
it2: AsyncIterable[T2],
|
|
345
|
+
it3: AsyncIterable[T3],
|
|
346
|
+
it4: AsyncIterable[T4],
|
|
347
|
+
it5: AsyncIterable[T5],
|
|
348
|
+
/,
|
|
349
|
+
*,
|
|
350
|
+
exc_handler: Callable[[BaseException], None] | None = None,
|
|
351
|
+
) -> ExecutorResult[R]: ...
|
|
352
|
+
|
|
353
|
+
def amap(
|
|
354
|
+
self,
|
|
355
|
+
func: Callable[..., Awaitable[R] | Coroutine[Any, Any, R]],
|
|
356
|
+
it: AsyncIterable[Any],
|
|
357
|
+
/,
|
|
358
|
+
*its: AsyncIterable[Any],
|
|
359
|
+
exc_handler: Callable[[BaseException], None] | None = None,
|
|
360
|
+
) -> ExecutorResult[R]:
|
|
361
|
+
"""
|
|
362
|
+
Executes an asynchronous function concurrently with
|
|
363
|
+
asynchronous iterables to use.
|
|
364
|
+
|
|
365
|
+
:param func: The function to run.
|
|
366
|
+
:param exc_handler: Provides an alternative solution
|
|
367
|
+
for handling given exceptions, if not selected
|
|
368
|
+
for this function will back off to whatever
|
|
369
|
+
the executor class has provided and if that
|
|
370
|
+
is not the case then the exception will
|
|
371
|
+
be raised.
|
|
372
|
+
|
|
373
|
+
:raise RuntimeError:
|
|
374
|
+
If executor has been previously shut down.
|
|
375
|
+
"""
|
|
376
|
+
if self.is_closed:
|
|
377
|
+
raise RuntimeError("Executor already closed.")
|
|
378
|
+
|
|
379
|
+
queue: SimpleQueue[T] = SimpleQueue()
|
|
380
|
+
cd = CountdownEvent()
|
|
381
|
+
cd.up()
|
|
382
|
+
queue: SimpleQueue[T] = SimpleQueue()
|
|
383
|
+
self._tg.start_soon(
|
|
384
|
+
self._load_tasks_async_iter,
|
|
385
|
+
queue,
|
|
386
|
+
cd,
|
|
387
|
+
func,
|
|
388
|
+
exc_handler or self._exc_handler,
|
|
389
|
+
it,
|
|
390
|
+
*its,
|
|
391
|
+
)
|
|
392
|
+
return ExecutorResult(queue, cd, self)
|
|
393
|
+
|
|
394
|
+
async def shutdown(self) -> None:
|
|
395
|
+
"""Shuts down all running executor tasks"""
|
|
396
|
+
if not self.is_closed:
|
|
397
|
+
for _ in range(self._num_workers):
|
|
398
|
+
# Set all workers to completed...
|
|
399
|
+
await self._work_queue.async_put(None)
|
|
400
|
+
await self._workers_done
|
|
401
|
+
|
|
402
|
+
async def __aexit__(
|
|
403
|
+
self,
|
|
404
|
+
exc_type: type[BaseException] | None = None,
|
|
405
|
+
exc_val: BaseException | None = None,
|
|
406
|
+
exc_tb: TracebackType | None = None,
|
|
407
|
+
):
|
|
408
|
+
await self.shutdown()
|
|
409
|
+
return await self._tg.__aexit__(exc_type, exc_val, exc_tb)
|
|
410
|
+
|
|
411
|
+
|
|
412
|
+
__all__ = ("Executor", "ExecutorResult")
|
anyio_executor/py.typed
ADDED
|
File without changes
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: anyio-executor
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: anyio implementation of an asynchronous Executor.
|
|
5
|
+
Author-email: Vizonex <VizonexBusiness@gmail.com>
|
|
6
|
+
Project-URL: repository, https://github.com/Vizonex/anyio-executor.git
|
|
7
|
+
Requires-Python: >=3.10
|
|
8
|
+
Description-Content-Type: text/markdown
|
|
9
|
+
License-File: LICENSE
|
|
10
|
+
Requires-Dist: aiologic>=0.16.0
|
|
11
|
+
Requires-Dist: anyio>=4.13.0
|
|
12
|
+
Provides-Extra: test
|
|
13
|
+
Requires-Dist: pytest; extra == "test"
|
|
14
|
+
Dynamic: license-file
|
|
15
|
+
|
|
16
|
+
# Anyio-Executor
|
|
17
|
+
Inspired by the newer [aiolibs-executor](https://github.com/aio-libs/aiolibs-executor) library
|
|
18
|
+
this library provides an asynchronous executor support for anyio and has compatability
|
|
19
|
+
with 3.10 and newer version of python.
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
```python
|
|
23
|
+
import anyio
|
|
24
|
+
|
|
25
|
+
from anyio_executor import Executor
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
async def fn(i: int):
|
|
29
|
+
await anyio.sleep(i)
|
|
30
|
+
print(f"{i} done!")
|
|
31
|
+
return i
|
|
32
|
+
|
|
33
|
+
async def main():
|
|
34
|
+
async with Executor(10) as e:
|
|
35
|
+
items = await e.map(fn, [1, 2, 1, 2, 3])
|
|
36
|
+
print(items)
|
|
37
|
+
|
|
38
|
+
async with Executor(10) as e:
|
|
39
|
+
# it will also take async for iterations.
|
|
40
|
+
async for i in e.map(fn, [1, 2, 1, 2, 3]):
|
|
41
|
+
print(i)
|
|
42
|
+
|
|
43
|
+
if __name__ == "__main__":
|
|
44
|
+
anyio.run(main)
|
|
45
|
+
```
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
anyio_executor/__init__.py,sha256=bHgz3GxFbKmljOVLPRTo2YxjsTYdUQsT6Sf7SjIwXW4,131
|
|
2
|
+
anyio_executor/azip.py,sha256=ZGORMy5TQXji5WyEQfi2vslY_on4tYPK8FGMRb40LpE,3397
|
|
3
|
+
anyio_executor/core.py,sha256=Ymv6ry_kANQh83hC6ygzFxD6GGSRuUxKT6kvAKn4Egk,12148
|
|
4
|
+
anyio_executor/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
5
|
+
anyio_executor-0.1.0.dist-info/licenses/LICENSE,sha256=PvIf1TAjSa17WPzFHoC_rIwnh1GqL5Nf_FXyWsrE6uk,11333
|
|
6
|
+
anyio_executor-0.1.0.dist-info/METADATA,sha256=jUt3tU8Lmi64AkM7gy3gSd1H8gEa_vAx2f9jbKqboUI,1182
|
|
7
|
+
anyio_executor-0.1.0.dist-info/WHEEL,sha256=aeYiig01lYGDzBgS8HxWXOg3uV61G9ijOsup-k9o1sk,91
|
|
8
|
+
anyio_executor-0.1.0.dist-info/top_level.txt,sha256=pMuUAobtC9xXGNtzWh2bGg9Ei8h4gY_CKpTyNzR1S3Q,15
|
|
9
|
+
anyio_executor-0.1.0.dist-info/RECORD,,
|
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
anyio_executor
|