Signal
signal ¶
Signal stopper for graceful evolution termination on Unix signals.
This module provides a SignalStopper that handles Unix signals (SIGINT, SIGTERM) for graceful shutdown of evolution runs. Supports both async and sync contexts with proper signal handler management.
| ATTRIBUTE | DESCRIPTION |
|---|---|
SignalStopper | Stop evolution on Unix signals (SIGINT, SIGTERM). TYPE: |
Examples:
Using as a context manager (recommended):
from gepa_adk.adapters.stoppers import SignalStopper
async with SignalStopper() as stopper:
config = EvolutionConfig(stop_callbacks=[stopper])
result = await engine.run(config)
# Ctrl+C will gracefully stop evolution
Manual setup and cleanup:
Note
This stopper requires careful integration with asyncio's event loop for proper signal handling in async contexts.
SignalStopper ¶
Stop evolution on Unix signals (SIGINT, SIGTERM).
Handles Ctrl+C (SIGINT) and termination signals gracefully, allowing the current iteration to complete before returning results. Supports both async contexts (using asyncio signal handling) and sync contexts (using traditional signal handlers).
| ATTRIBUTE | DESCRIPTION |
|---|---|
signals | Signals to handle. TYPE: |
Examples:
Using as async context manager:
from gepa_adk.adapters.stoppers import SignalStopper
async with SignalStopper() as stopper:
# stopper.setup() called automatically
config = EvolutionConfig(stop_callbacks=[stopper])
result = await engine.run(config)
# stopper.cleanup() called automatically
Using with custom signals:
import signal
stopper = SignalStopper(signals=[signal.SIGINT])
stopper.setup()
try:
# Only SIGINT will trigger stop
pass
finally:
stopper.cleanup()
Note
Always call cleanup() after evolution completes to restore original signal handlers. The context manager pattern handles this automatically.
Source code in src/gepa_adk/adapters/stoppers/signal.py
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 | |
__init__ ¶
Initialize signal stopper with signals to handle.
| PARAMETER | DESCRIPTION |
|---|---|
signals | Signals to handle. Defaults to [SIGINT, SIGTERM] which covers Ctrl+C and system termination requests. TYPE: |
Examples:
# Default signals (SIGINT, SIGTERM)
stopper = SignalStopper()
# Custom signals
stopper = SignalStopper(signals=[signal.SIGINT])
Note
Call setup() before evolution starts and cleanup() after evolution completes to properly manage signal handlers.
Source code in src/gepa_adk/adapters/stoppers/signal.py
setup ¶
Install signal handlers.
Must be called before evolution starts. In async contexts, uses asyncio's signal handling. In sync contexts, uses traditional signal handlers.
Examples:
Note
On platforms where certain signals are unavailable (like Windows for SIGTERM), those signals are silently skipped.
Source code in src/gepa_adk/adapters/stoppers/signal.py
__call__ ¶
__call__(state: StopperState) -> bool
Check if evolution should stop due to signal.
| PARAMETER | DESCRIPTION |
|---|---|
state | Current evolution state snapshot (not used, but required by StopperProtocol). TYPE: |
| RETURNS | DESCRIPTION |
|---|---|
bool | True if a signal was received, False otherwise. |
Examples:
stopper = SignalStopper()
stopper.setup()
state = StopperState(
iteration=5,
best_score=0.8,
stagnation_counter=0,
total_evaluations=25,
candidates_count=1,
elapsed_seconds=30.0,
)
# Before signal
stopper(state) # False
# After Ctrl+C
# stopper(state) # True
Note
Often called after each iteration. Returns True as soon as a signal is received.
Source code in src/gepa_adk/adapters/stoppers/signal.py
cleanup ¶
Restore original signal handlers.
Should be called after evolution completes to restore the signal handlers that were in place before setup() was called.
Examples:
stopper = SignalStopper()
stopper.setup()
try:
# Run evolution
pass
finally:
stopper.cleanup() # Restore original handlers
Note
On platforms where certain signals are unavailable, those signals are silently skipped during cleanup.
Source code in src/gepa_adk/adapters/stoppers/signal.py
__aenter__ async ¶
__aenter__() -> SignalStopper
Enter async context and install signal handlers.
| RETURNS | DESCRIPTION |
|---|---|
SignalStopper | Self for use as stopper in evolution config. |
Examples:
async with SignalStopper() as stopper:
config = EvolutionConfig(stop_callbacks=[stopper])
result = await engine.run(config)
Note
On entry, signal handlers are installed automatically.
Source code in src/gepa_adk/adapters/stoppers/signal.py
__aexit__ async ¶
__aexit__(
exc_type: type[BaseException] | None,
exc_val: BaseException | None,
exc_tb: object,
) -> None
Exit async context and restore signal handlers.
| PARAMETER | DESCRIPTION |
|---|---|
exc_type | Exception type if an exception was raised. TYPE: |
exc_val | Exception value if an exception was raised. TYPE: |
exc_tb | Exception traceback if an exception was raised. TYPE: |
Note
Original signal handlers are restored automatically on exit, even if an exception was raised.
Source code in src/gepa_adk/adapters/stoppers/signal.py
__enter__ ¶
__enter__() -> SignalStopper
Enter sync context and install signal handlers.
| RETURNS | DESCRIPTION |
|---|---|
SignalStopper | Self for use as stopper in evolution config. |
Examples:
with SignalStopper() as stopper:
config = EvolutionConfig(stop_callbacks=[stopper])
result = engine.run(config) # sync run
Note
On entry, signal handlers are installed automatically.
Source code in src/gepa_adk/adapters/stoppers/signal.py
__exit__ ¶
__exit__(
exc_type: type[BaseException] | None,
exc_val: BaseException | None,
exc_tb: object,
) -> None
Exit sync context and restore signal handlers.
| PARAMETER | DESCRIPTION |
|---|---|
exc_type | Exception type if an exception was raised. TYPE: |
exc_val | Exception value if an exception was raised. TYPE: |
exc_tb | Exception traceback if an exception was raised. TYPE: |
Note
Original signal handlers are restored automatically on exit, even if an exception was raised.