sunholo 0.100.2__py3-none-any.whl → 0.100.3__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.
- sunholo/invoke/async_class.py +143 -82
- {sunholo-0.100.2.dist-info → sunholo-0.100.3.dist-info}/METADATA +2 -2
- {sunholo-0.100.2.dist-info → sunholo-0.100.3.dist-info}/RECORD +7 -7
- {sunholo-0.100.2.dist-info → sunholo-0.100.3.dist-info}/LICENSE.txt +0 -0
- {sunholo-0.100.2.dist-info → sunholo-0.100.3.dist-info}/WHEEL +0 -0
- {sunholo-0.100.2.dist-info → sunholo-0.100.3.dist-info}/entry_points.txt +0 -0
- {sunholo-0.100.2.dist-info → sunholo-0.100.3.dist-info}/top_level.txt +0 -0
sunholo/invoke/async_class.py
CHANGED
|
@@ -1,63 +1,107 @@
|
|
|
1
1
|
import asyncio
|
|
2
|
-
from ..custom_logging import log
|
|
3
|
-
import traceback
|
|
4
2
|
from typing import Callable, Any, AsyncGenerator, Dict
|
|
3
|
+
import time
|
|
4
|
+
import traceback
|
|
5
|
+
from ..custom_logging import setup_logging
|
|
5
6
|
from tenacity import AsyncRetrying, retry_if_exception_type, wait_random_exponential, stop_after_attempt
|
|
6
7
|
|
|
8
|
+
log = setup_logging("sunholo_AsyncTaskRunner")
|
|
9
|
+
|
|
7
10
|
class AsyncTaskRunner:
|
|
8
11
|
def __init__(self, retry_enabled=False, retry_kwargs=None):
|
|
9
12
|
self.tasks = []
|
|
10
13
|
self.retry_enabled = retry_enabled
|
|
11
14
|
self.retry_kwargs = retry_kwargs or {}
|
|
12
|
-
|
|
15
|
+
|
|
13
16
|
def add_task(self, func: Callable[..., Any], *args: Any):
|
|
14
|
-
"""
|
|
17
|
+
"""
|
|
18
|
+
Adds a task to the list of tasks to be executed.
|
|
19
|
+
"""
|
|
15
20
|
log.info(f"Adding task: {func.__name__} with args: {args}")
|
|
16
21
|
self.tasks.append((func.__name__, func, args))
|
|
17
|
-
|
|
18
|
-
async def run_async_as_completed(self, callback=None) -> AsyncGenerator[Dict[str, Any], None]:
|
|
19
|
-
"""
|
|
20
|
-
Runs all tasks concurrently and yields results as they complete, while periodically sending heartbeat messages.
|
|
21
22
|
|
|
22
|
-
|
|
23
|
-
|
|
23
|
+
async def run_async_as_completed(self) -> AsyncGenerator[Dict[str, Any], None]:
|
|
24
|
+
"""
|
|
25
|
+
Runs all tasks concurrently and yields results as they complete,
|
|
26
|
+
while periodically sending heartbeat messages.
|
|
24
27
|
"""
|
|
25
28
|
log.info("Running tasks asynchronously and yielding results as they complete")
|
|
26
|
-
|
|
29
|
+
|
|
30
|
+
# Create a queue for inter-coroutine communication
|
|
31
|
+
queue = asyncio.Queue()
|
|
32
|
+
|
|
33
|
+
# List to keep track of all running tasks and their heartbeats
|
|
34
|
+
task_infos = []
|
|
35
|
+
|
|
36
|
+
# Start all tasks and their corresponding heartbeats
|
|
27
37
|
for name, func, args in self.tasks:
|
|
28
|
-
#
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
38
|
+
# Create an event to signal task completion to the heartbeat
|
|
39
|
+
completion_event = asyncio.Event()
|
|
40
|
+
|
|
41
|
+
# Start the main task with retries
|
|
42
|
+
task_coro = self._run_with_retries(name, func, *args, queue=queue, completion_event=completion_event)
|
|
43
|
+
task = asyncio.create_task(task_coro)
|
|
44
|
+
|
|
45
|
+
# Start the heartbeat coroutine
|
|
46
|
+
heartbeat_coro = self._send_heartbeat(name, completion_event, queue)
|
|
47
|
+
heartbeat_task = asyncio.create_task(heartbeat_coro)
|
|
48
|
+
|
|
49
|
+
# Store task information for management
|
|
50
|
+
task_infos.append({
|
|
51
|
+
'name': name,
|
|
52
|
+
'task': task,
|
|
53
|
+
'heartbeat_task': heartbeat_task,
|
|
54
|
+
'completion_event': completion_event
|
|
55
|
+
})
|
|
56
|
+
|
|
57
|
+
log.info(f"Started task '{name}' and its heartbeat")
|
|
58
|
+
|
|
59
|
+
log.info(f"Started async run with {len(self.tasks)} tasks and heartbeats")
|
|
60
|
+
|
|
61
|
+
# Create a monitor task to detect when all tasks and heartbeats are done
|
|
62
|
+
monitor = asyncio.create_task(self._monitor_tasks(task_infos, queue))
|
|
63
|
+
|
|
64
|
+
# Continuously yield messages from the queue until sentinel is received
|
|
65
|
+
while True:
|
|
66
|
+
message = await queue.get()
|
|
67
|
+
if message is None:
|
|
68
|
+
log.info("Received sentinel. Exiting message loop.")
|
|
69
|
+
break # Sentinel received, all tasks and heartbeats are done
|
|
70
|
+
log.info(f"Received message from queue: {message}")
|
|
71
|
+
yield message
|
|
55
72
|
|
|
56
|
-
#
|
|
57
|
-
|
|
58
|
-
if callback:
|
|
59
|
-
heartbeat_task = asyncio.create_task(self._send_heartbeat(callback, name))
|
|
73
|
+
# Wait for the monitor to finish
|
|
74
|
+
await monitor
|
|
60
75
|
|
|
76
|
+
log.info("All tasks and heartbeats have completed")
|
|
77
|
+
|
|
78
|
+
async def _monitor_tasks(self, task_infos, queue):
|
|
79
|
+
"""
|
|
80
|
+
Monitors the tasks and heartbeats, and sends a sentinel to the queue when done.
|
|
81
|
+
"""
|
|
82
|
+
# Wait for all main tasks to complete
|
|
83
|
+
main_tasks = [info['task'] for info in task_infos]
|
|
84
|
+
log.info("Monitor: Waiting for all main tasks to complete")
|
|
85
|
+
await asyncio.gather(*main_tasks, return_exceptions=True)
|
|
86
|
+
log.info("Monitor: All main tasks have completed")
|
|
87
|
+
|
|
88
|
+
# Cancel all heartbeat tasks
|
|
89
|
+
for info in task_infos:
|
|
90
|
+
info['heartbeat_task'].cancel()
|
|
91
|
+
try:
|
|
92
|
+
await info['heartbeat_task']
|
|
93
|
+
except asyncio.CancelledError:
|
|
94
|
+
pass
|
|
95
|
+
log.info(f"Monitor: Heartbeat for task '{info['name']}' has been canceled")
|
|
96
|
+
|
|
97
|
+
# Send a sentinel to indicate completion
|
|
98
|
+
await queue.put(None)
|
|
99
|
+
log.info("Monitor: Sent sentinel to queue")
|
|
100
|
+
|
|
101
|
+
async def _run_with_retries(self, name: str, func: Callable[..., Any], *args: Any, queue: asyncio.Queue, completion_event: asyncio.Event) -> None:
|
|
102
|
+
"""
|
|
103
|
+
Executes a task with optional retries and sends completion or error messages to the queue.
|
|
104
|
+
"""
|
|
61
105
|
try:
|
|
62
106
|
if self.retry_enabled:
|
|
63
107
|
retry_kwargs = {
|
|
@@ -68,50 +112,67 @@ class AsyncTaskRunner:
|
|
|
68
112
|
}
|
|
69
113
|
async for attempt in AsyncRetrying(**retry_kwargs):
|
|
70
114
|
with attempt:
|
|
71
|
-
|
|
115
|
+
log.info(f"Starting task '{name}' with retry")
|
|
116
|
+
result = await self._execute_task(func, *args)
|
|
117
|
+
await queue.put({'type': 'task_complete', 'func_name': name, 'result': result})
|
|
118
|
+
log.info(f"Sent 'task_complete' message for task '{name}'")
|
|
119
|
+
return
|
|
72
120
|
else:
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
121
|
+
log.info(f"Starting task '{name}' with no retry")
|
|
122
|
+
result = await self._execute_task(func, *args)
|
|
123
|
+
await queue.put({'type': 'task_complete', 'func_name': name, 'result': result})
|
|
124
|
+
log.info(f"Sent 'task_complete' message for task '{name}'")
|
|
125
|
+
except Exception as e:
|
|
126
|
+
log.error(f"Error in task '{name}': {e}\n{traceback.format_exc()}")
|
|
127
|
+
await queue.put({'type': 'task_error', 'func_name': name, 'error': f'{e}\n{traceback.format_exc()}'})
|
|
128
|
+
log.info(f"Sent 'task_error' message for task '{name}'")
|
|
78
129
|
finally:
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
130
|
+
log.info(f"Task '{name}' completed.")
|
|
131
|
+
# Set the completion event after sending the message
|
|
132
|
+
completion_event.set()
|
|
133
|
+
|
|
134
|
+
async def _execute_task(self, func: Callable[..., Any], *args: Any) -> Any:
|
|
135
|
+
"""
|
|
136
|
+
Executes the given task function and returns its result.
|
|
137
|
+
|
|
138
|
+
Args:
|
|
139
|
+
func (Callable): The callable to execute.
|
|
140
|
+
*args: Arguments to pass to the callable.
|
|
141
|
+
|
|
142
|
+
Returns:
|
|
143
|
+
Any: The result of the task.
|
|
144
|
+
"""
|
|
145
|
+
if asyncio.iscoroutinefunction(func):
|
|
146
|
+
return await func(*args)
|
|
147
|
+
else:
|
|
148
|
+
return await asyncio.to_thread(func, *args)
|
|
149
|
+
|
|
150
|
+
async def _send_heartbeat(self, func_name: str, completion_event: asyncio.Event, queue: asyncio.Queue, interval: int = 2):
|
|
97
151
|
"""
|
|
98
152
|
Sends periodic heartbeat updates to indicate the task is still in progress.
|
|
99
153
|
|
|
100
154
|
Args:
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
155
|
+
func_name (str): The name of the task function.
|
|
156
|
+
completion_event (asyncio.Event): Event to signal when the task is completed.
|
|
157
|
+
queue (asyncio.Queue): The queue to send heartbeat messages to.
|
|
158
|
+
interval (int): How frequently to send heartbeat messages (in seconds).
|
|
104
159
|
"""
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
160
|
+
start_time = time.time()
|
|
161
|
+
log.info(f"Starting heartbeat for task '{func_name}' with interval {interval} seconds")
|
|
162
|
+
try:
|
|
163
|
+
while not completion_event.is_set():
|
|
164
|
+
await asyncio.sleep(interval)
|
|
165
|
+
elapsed_time = int(time.time() - start_time)
|
|
166
|
+
heartbeat_message = {
|
|
167
|
+
'type': 'heartbeat',
|
|
168
|
+
'name': func_name,
|
|
169
|
+
'interval': interval,
|
|
170
|
+
'elapsed_time': elapsed_time
|
|
171
|
+
}
|
|
172
|
+
log.info(f"Sending heartbeat for task '{func_name}', running for {elapsed_time} seconds")
|
|
173
|
+
await queue.put(heartbeat_message)
|
|
174
|
+
except asyncio.CancelledError:
|
|
175
|
+
log.info(f"Heartbeat for task '{func_name}' has been canceled")
|
|
176
|
+
finally:
|
|
177
|
+
log.info(f"Heartbeat for task '{func_name}' stopped")
|
|
178
|
+
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
Metadata-Version: 2.1
|
|
2
2
|
Name: sunholo
|
|
3
|
-
Version: 0.100.
|
|
3
|
+
Version: 0.100.3
|
|
4
4
|
Summary: Large Language Model DevOps - a package to help deploy LLMs to the Cloud.
|
|
5
5
|
Home-page: https://github.com/sunholo-data/sunholo-py
|
|
6
|
-
Download-URL: https://github.com/sunholo-data/sunholo-py/archive/refs/tags/v0.100.
|
|
6
|
+
Download-URL: https://github.com/sunholo-data/sunholo-py/archive/refs/tags/v0.100.3.tar.gz
|
|
7
7
|
Author: Holosun ApS
|
|
8
8
|
Author-email: multivac@sunholo.com
|
|
9
9
|
License: Apache License, Version 2.0
|
|
@@ -89,7 +89,7 @@ sunholo/genai/init.py,sha256=yG8E67TduFCTQPELo83OJuWfjwTnGZsyACospahyEaY,687
|
|
|
89
89
|
sunholo/genai/process_funcs_cls.py,sha256=DPe70E71pofInLMFBFdudFZr0ZCFHN1LFCQjpxtG8xU,26552
|
|
90
90
|
sunholo/genai/safety.py,sha256=mkFDO_BeEgiKjQd9o2I4UxB6XI7a9U-oOFjZ8LGRUC4,1238
|
|
91
91
|
sunholo/invoke/__init__.py,sha256=o1RhwBGOtVK0MIdD55fAIMCkJsxTksi8GD5uoqVKI-8,184
|
|
92
|
-
sunholo/invoke/async_class.py,sha256=
|
|
92
|
+
sunholo/invoke/async_class.py,sha256=Wkrv3exjN3aDQdI1Q4tv8I8xSloKgdz_LzGomayfLhw,7734
|
|
93
93
|
sunholo/invoke/direct_vac_func.py,sha256=GXSCMkC6vOWGUtQjxy-ZpTrMvJa3CgcW-y9mDpJwWC8,9533
|
|
94
94
|
sunholo/invoke/invoke_vac_utils.py,sha256=sJc1edHTHMzMGXjji1N67c3iUaP7BmAL5nj82Qof63M,2053
|
|
95
95
|
sunholo/langfuse/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
@@ -144,9 +144,9 @@ sunholo/vertex/init.py,sha256=1OQwcPBKZYBTDPdyU7IM4X4OmiXLdsNV30C-fee2scQ,2875
|
|
|
144
144
|
sunholo/vertex/memory_tools.py,sha256=tBZxqVZ4InTmdBvLlOYwoSEWu4-kGquc-gxDwZCC4FA,7667
|
|
145
145
|
sunholo/vertex/safety.py,sha256=S9PgQT1O_BQAkcqauWncRJaydiP8Q_Jzmu9gxYfy1VA,2482
|
|
146
146
|
sunholo/vertex/type_dict_to_json.py,sha256=uTzL4o9tJRao4u-gJOFcACgWGkBOtqACmb6ihvCErL8,4694
|
|
147
|
-
sunholo-0.100.
|
|
148
|
-
sunholo-0.100.
|
|
149
|
-
sunholo-0.100.
|
|
150
|
-
sunholo-0.100.
|
|
151
|
-
sunholo-0.100.
|
|
152
|
-
sunholo-0.100.
|
|
147
|
+
sunholo-0.100.3.dist-info/LICENSE.txt,sha256=SdE3QjnD3GEmqqg9EX3TM9f7WmtOzqS1KJve8rhbYmU,11345
|
|
148
|
+
sunholo-0.100.3.dist-info/METADATA,sha256=c5y87oJoR39hmBJfM3dGVuHKI1e2A5ZJhTyrFaTOSbQ,8312
|
|
149
|
+
sunholo-0.100.3.dist-info/WHEEL,sha256=GV9aMThwP_4oNCtvEC2ec3qUYutgWeAzklro_0m4WJQ,91
|
|
150
|
+
sunholo-0.100.3.dist-info/entry_points.txt,sha256=bZuN5AIHingMPt4Ro1b_T-FnQvZ3teBes-3OyO0asl4,49
|
|
151
|
+
sunholo-0.100.3.dist-info/top_level.txt,sha256=wt5tadn5--5JrZsjJz2LceoUvcrIvxjHJe-RxuudxAk,8
|
|
152
|
+
sunholo-0.100.3.dist-info/RECORD,,
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|