utg-base 1.6.1__py3-none-any.whl → 1.7.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.
@@ -0,0 +1,67 @@
1
+ import multiprocessing
2
+ from concurrent.futures import ThreadPoolExecutor, as_completed
3
+ from typing import Callable, Any, Iterable, Tuple, Dict, Union
4
+
5
+
6
+ class ThreadPoolException(Exception):
7
+ """Exception to wrap the error in each task."""
8
+
9
+ def __init__(self, func: str, exception: Exception):
10
+ self.func = func
11
+ self.exception = exception
12
+ super().__init__(f"Exception in {func}: {exception}")
13
+
14
+
15
+ def parallel_execute(
16
+ *tasks: Union[
17
+ Callable, # func
18
+ Tuple[Callable, Iterable], # (func, args)
19
+ Tuple[Callable, Iterable, Dict[str, Any]] # (func, args, kwargs)
20
+ ],
21
+ max_workers: int = None
22
+ ) -> list[Any]:
23
+ """
24
+ Executes multiple functions in parallel.
25
+ Each can be without arguments or with args/kwargs.
26
+
27
+ Example:
28
+ parallel_execute(func1, func2)
29
+ parallel_execute((func1, (1, 2)), (func2, (), {"x": 5}))
30
+
31
+ The sequence is preserved.
32
+
33
+ :return: Result list or ThreadPoolException objects
34
+ """
35
+ if not tasks:
36
+ return []
37
+
38
+ cpu_count = multiprocessing.cpu_count()
39
+ max_workers = max_workers or min(cpu_count * 10, len(tasks))
40
+ results = [None] * len(tasks)
41
+
42
+ with ThreadPoolExecutor(max_workers=max_workers) as executor:
43
+ futures = {}
44
+
45
+ for i, task in enumerate(tasks):
46
+ # Task unpacking
47
+ if callable(task):
48
+ func, args, kwargs = task, (), {}
49
+ elif isinstance(task, tuple):
50
+ func = task[0]
51
+ args = task[1] if len(task) > 1 else ()
52
+ kwargs = task[2] if len(task) > 2 else {}
53
+ else:
54
+ raise ValueError(f"Invalid task format: {task}")
55
+
56
+ futures[executor.submit(func, *args, **kwargs)] = i
57
+
58
+ for future in as_completed(futures):
59
+ idx = futures[future]
60
+ func = tasks[idx][0] if isinstance(tasks[idx], tuple) else tasks[idx]
61
+ func_name = getattr(func, "__name__", str(func))
62
+ try:
63
+ results[idx] = future.result()
64
+ except Exception as e:
65
+ results[idx] = ThreadPoolException(func=func_name, exception=e)
66
+
67
+ return results
@@ -1,9 +1,9 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: utg-base
3
- Version: 1.6.1
3
+ Version: 1.7.0
4
4
  Summary: UTG Base Package
5
- Author: Rovshen
6
- Author-email: rovshenashirov1619@gmail.com
5
+ Author: Olimboy
6
+ Author-email: shavkatov.olimboy@mail.ru
7
7
  Requires-Python: >=3.14,<4.0
8
8
  Classifier: Programming Language :: Python :: 3
9
9
  Requires-Dist: celery (>=5.5.3,<6.0.0)
@@ -49,7 +49,8 @@ utg_base/utils/date.py,sha256=thcbK6RgTUYZfs4_vW5ucuu2e8H0rei6tv7SEC72iwM,3612
49
49
  utg_base/utils/dict_util.py,sha256=ipdCZO8aTukGQ319OWHb2Ij5MNtV-FioJQ4qCX3Th48,758
50
50
  utg_base/utils/response_processors.py,sha256=WdZQL49wOJqCIY2MucAI6sez_llCqih0v_ltQa-mv7k,687
51
51
  utg_base/utils/sql.py,sha256=rqIWcSjdjIMszdRnsnhV5TTYB8W17RPOujIQA9rKC_Y,762
52
+ utg_base/utils/thread.py,sha256=4RqRnwtyHymY-dNcuPrMSTamE2V7wCMVfzzyIb0P4TI,2191
52
53
  utg_base/utils/translation.py,sha256=GxJHUt0iar_0E7RWBPbeLFQ4DhgXBjffHCmxfKyjFtk,463
53
- utg_base-1.6.1.dist-info/METADATA,sha256=VFG9y4kzasChqU2btKkXSwCifVjnxEoaIFnIV9bAhlw,874
54
- utg_base-1.6.1.dist-info/WHEEL,sha256=Nq82e9rUAnEjt98J6MlVmMCZb-t9cYE2Ir1kpBmnWfs,88
55
- utg_base-1.6.1.dist-info/RECORD,,
54
+ utg_base-1.7.0.dist-info/METADATA,sha256=1XwqfOqDxhuzkk0Rfk5ZRjasjzBHIvYxfA2PuhFO9pk,871
55
+ utg_base-1.7.0.dist-info/WHEEL,sha256=Nq82e9rUAnEjt98J6MlVmMCZb-t9cYE2Ir1kpBmnWfs,88
56
+ utg_base-1.7.0.dist-info/RECORD,,