haiway 0.19.4__py3-none-any.whl → 0.19.5__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.
- haiway/__init__.py +2 -0
- haiway/helpers/__init__.py +2 -0
- haiway/helpers/concurrent.py +74 -0
- {haiway-0.19.4.dist-info → haiway-0.19.5.dist-info}/METADATA +1 -1
- {haiway-0.19.4.dist-info → haiway-0.19.5.dist-info}/RECORD +7 -6
- {haiway-0.19.4.dist-info → haiway-0.19.5.dist-info}/WHEEL +0 -0
- {haiway-0.19.4.dist-info → haiway-0.19.5.dist-info}/licenses/LICENSE +0 -0
haiway/__init__.py
CHANGED
@@ -22,6 +22,7 @@ from haiway.helpers import (
|
|
22
22
|
LoggerObservability,
|
23
23
|
asynchronous,
|
24
24
|
cache,
|
25
|
+
process_concurrently,
|
25
26
|
retry,
|
26
27
|
throttle,
|
27
28
|
timeout,
|
@@ -112,6 +113,7 @@ __all__ = (
|
|
112
113
|
"mimic_function",
|
113
114
|
"noop",
|
114
115
|
"not_missing",
|
116
|
+
"process_concurrently",
|
115
117
|
"retry",
|
116
118
|
"setup_logging",
|
117
119
|
"throttle",
|
haiway/helpers/__init__.py
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
from haiway.helpers.asynchrony import asynchronous, wrap_async
|
2
2
|
from haiway.helpers.caching import CacheMakeKey, CacheRead, CacheWrite, cache
|
3
|
+
from haiway.helpers.concurrent import process_concurrently
|
3
4
|
from haiway.helpers.observability import LoggerObservability
|
4
5
|
from haiway.helpers.retries import retry
|
5
6
|
from haiway.helpers.throttling import throttle
|
@@ -13,6 +14,7 @@ __all__ = (
|
|
13
14
|
"LoggerObservability",
|
14
15
|
"asynchronous",
|
15
16
|
"cache",
|
17
|
+
"process_concurrently",
|
16
18
|
"retry",
|
17
19
|
"throttle",
|
18
20
|
"timeout",
|
@@ -0,0 +1,74 @@
|
|
1
|
+
from asyncio import FIRST_COMPLETED, CancelledError, Task, wait
|
2
|
+
from collections.abc import AsyncIterator, Callable, Coroutine
|
3
|
+
from concurrent.futures import ALL_COMPLETED
|
4
|
+
from typing import Any
|
5
|
+
|
6
|
+
from haiway.context import ctx
|
7
|
+
|
8
|
+
__all__ = ("process_concurrently",)
|
9
|
+
|
10
|
+
|
11
|
+
async def process_concurrently[Element]( # noqa: C901
|
12
|
+
source: AsyncIterator[Element],
|
13
|
+
/,
|
14
|
+
handler: Callable[[Element], Coroutine[Any, Any, None]],
|
15
|
+
*,
|
16
|
+
concurrent_tasks: int = 2,
|
17
|
+
ignore_exceptions: bool = False,
|
18
|
+
) -> None:
|
19
|
+
"""Process elements from an async iterator concurrently.
|
20
|
+
|
21
|
+
Parameters
|
22
|
+
----------
|
23
|
+
source: AsyncIterator[Element]
|
24
|
+
An async iterator providing elements to process.
|
25
|
+
|
26
|
+
handler: Callable[[Element], Coroutine[Any, Any, None]]
|
27
|
+
A coroutine function that processes each element.
|
28
|
+
|
29
|
+
concurrent_tasks: int
|
30
|
+
Maximum number of concurrent tasks (must be > 0), default is 2.
|
31
|
+
|
32
|
+
ignore_exceptions: bool
|
33
|
+
If True, exceptions from tasks will be logged but not propagated,
|
34
|
+
default is False.
|
35
|
+
|
36
|
+
"""
|
37
|
+
assert concurrent_tasks > 0 # nosec: B101
|
38
|
+
running: set[Task[None]] = set()
|
39
|
+
try:
|
40
|
+
while element := await anext(source, None):
|
41
|
+
if len(running) < concurrent_tasks:
|
42
|
+
running.add(ctx.spawn(handler, element))
|
43
|
+
continue # keep spawning tasks
|
44
|
+
|
45
|
+
completed, running = await wait(running, return_when=FIRST_COMPLETED)
|
46
|
+
|
47
|
+
for task in completed:
|
48
|
+
if exc := task.exception():
|
49
|
+
if not ignore_exceptions:
|
50
|
+
raise exc
|
51
|
+
|
52
|
+
ctx.log_error(
|
53
|
+
f"Concurrent processing error - {type(exc)}: {exc}",
|
54
|
+
exception=exc,
|
55
|
+
)
|
56
|
+
|
57
|
+
except CancelledError as exc:
|
58
|
+
# Cancel all running tasks
|
59
|
+
for task in running:
|
60
|
+
task.cancel()
|
61
|
+
|
62
|
+
raise exc
|
63
|
+
|
64
|
+
finally:
|
65
|
+
completed, _ = await wait(running, return_when=ALL_COMPLETED)
|
66
|
+
for task in completed:
|
67
|
+
if exc := task.exception():
|
68
|
+
if not ignore_exceptions:
|
69
|
+
raise exc
|
70
|
+
|
71
|
+
ctx.log_error(
|
72
|
+
f"Concurrent processing error - {type(exc)}: {exc}",
|
73
|
+
exception=exc,
|
74
|
+
)
|
@@ -1,6 +1,6 @@
|
|
1
1
|
Metadata-Version: 2.4
|
2
2
|
Name: haiway
|
3
|
-
Version: 0.19.
|
3
|
+
Version: 0.19.5
|
4
4
|
Summary: Framework for dependency injection and state management within structured concurrency model.
|
5
5
|
Project-URL: Homepage, https://miquido.com
|
6
6
|
Project-URL: Repository, https://github.com/miquido/haiway.git
|
@@ -1,4 +1,4 @@
|
|
1
|
-
haiway/__init__.py,sha256=
|
1
|
+
haiway/__init__.py,sha256=bFRmunbEmBzJy5iH_2WoMzvwhApRk3eo37b23zBe6Bc,2407
|
2
2
|
haiway/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
3
3
|
haiway/context/__init__.py,sha256=eoxqxUmFtkWLhc_gH6tqax9m90tSDie-jiP1BHruTdk,1102
|
4
4
|
haiway/context/access.py,sha256=vTh5BlduAGBjqnVyTgTUt08DG6SrnAakweAfIDBqx-0,18838
|
@@ -8,9 +8,10 @@ haiway/context/observability.py,sha256=u9NPejZGyGupqx3eH9IQKbcVcCFpZrg2ZAiov2FQi
|
|
8
8
|
haiway/context/state.py,sha256=0oq7ctNO0urJd7rVzwwNtgpguoXuI6Tp1exfCsxrS2M,5981
|
9
9
|
haiway/context/tasks.py,sha256=QOxFdjmMp4IYff0ihHElKLCQrcVksSJmxqTlOKfoH4o,2907
|
10
10
|
haiway/context/types.py,sha256=WulPvpqUbI1vYyny-s2NItldDnk3zh1O-n_hGibFZRY,142
|
11
|
-
haiway/helpers/__init__.py,sha256=
|
11
|
+
haiway/helpers/__init__.py,sha256=_A-JYAPemd-2o4NxxchaZENu8gANL8QDxK5ADzInXUU,670
|
12
12
|
haiway/helpers/asynchrony.py,sha256=k_A0yCWUKSFfzYZ8WvqK4wqTMljv6ykMivmERrDLHIU,6266
|
13
13
|
haiway/helpers/caching.py,sha256=N60FBFfSoOdSUI6S49l47G6T1j-y9tc3qhU5msSRdPM,13221
|
14
|
+
haiway/helpers/concurrent.py,sha256=04pWpS7qOQDD1pa2ifhFwedPcl4sRinhe-KgyDe5GF0,2296
|
14
15
|
haiway/helpers/observability.py,sha256=hahA7jgGj5TH6mYBm3bKVwI22Fseuh-IX6X487gueaU,7977
|
15
16
|
haiway/helpers/retries.py,sha256=unssUKBDOENvquh6R4Ud65TuSKl4mTHgZ5N_b7mAYa4,7533
|
16
17
|
haiway/helpers/throttling.py,sha256=U6HJvSzffw47730VeiXxXSW4VVxpDx48k0oIAOpL-O4,4115
|
@@ -39,7 +40,7 @@ haiway/utils/mimic.py,sha256=L5AS4WEL2aPMZAQZlvLvRzHl0cipI7ivky60_eL4iwY,1822
|
|
39
40
|
haiway/utils/noop.py,sha256=f54PSLHGEwCQNYXQHkPAW5NDE-tk5yjzkNL1pZj0TJQ,344
|
40
41
|
haiway/utils/queue.py,sha256=YTvCn3wgSwLJiLqolMx44sa3304Xkv3tJG77gvfWnZs,4114
|
41
42
|
haiway/utils/stream.py,sha256=Mjhy2S-ZDR1g_NsgS_nuBA8AgVbhrGXKvG3wjJ5mCJQ,2826
|
42
|
-
haiway-0.19.
|
43
|
-
haiway-0.19.
|
44
|
-
haiway-0.19.
|
45
|
-
haiway-0.19.
|
43
|
+
haiway-0.19.5.dist-info/METADATA,sha256=9zl0LfMAvGxzSzK7owry2SMQfSnitr3LUH6_UeqBqkI,4527
|
44
|
+
haiway-0.19.5.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
|
45
|
+
haiway-0.19.5.dist-info/licenses/LICENSE,sha256=3phcpHVNBP8jsi77gOO0E7rgKeDeu99Pi7DSnK9YHoQ,1069
|
46
|
+
haiway-0.19.5.dist-info/RECORD,,
|
File without changes
|
File without changes
|