|
13 | 13 | import re |
14 | 14 | import subprocess as sub |
15 | 15 | import sys |
| 16 | +import threading |
16 | 17 | import warnings |
17 | 18 | from abc import ABC, abstractmethod |
18 | 19 | from collections import namedtuple |
|
56 | 57 |
|
57 | 58 | which = "where" if os.name == "nt" else "which" |
58 | 59 |
|
| 60 | + |
| 61 | +def _run_highs_with_keyboard_interrupt(h: Any) -> None: |
| 62 | + """ |
| 63 | + Run `highspy.Highs.run()` while ensuring Ctrl-C cancels the solve. |
| 64 | +
|
| 65 | + HiGHS can run for a long time inside a C-extension call. Running it in a |
| 66 | + worker thread allows the main thread to reliably receive KeyboardInterrupt |
| 67 | + and signal HiGHS to stop via `cancelSolve()`. |
| 68 | + """ |
| 69 | + |
| 70 | + handle_keyboard_interrupt = getattr(h, "HandleKeyboardInterrupt", None) |
| 71 | + handle_user_interrupt = getattr(h, "HandleUserInterrupt", None) |
| 72 | + |
| 73 | + old_handle_keyboard_interrupt = ( |
| 74 | + handle_keyboard_interrupt if not callable(handle_keyboard_interrupt) else None |
| 75 | + ) |
| 76 | + old_handle_user_interrupt = ( |
| 77 | + handle_user_interrupt if not callable(handle_user_interrupt) else None |
| 78 | + ) |
| 79 | + |
| 80 | + try: |
| 81 | + if callable(handle_keyboard_interrupt): |
| 82 | + handle_keyboard_interrupt(True) |
| 83 | + elif handle_keyboard_interrupt is not None: |
| 84 | + h.HandleKeyboardInterrupt = True |
| 85 | + |
| 86 | + if callable(handle_user_interrupt): |
| 87 | + handle_user_interrupt(True) |
| 88 | + elif handle_user_interrupt is not None: |
| 89 | + h.HandleUserInterrupt = True |
| 90 | + |
| 91 | + finished = threading.Event() |
| 92 | + run_error: BaseException | None = None |
| 93 | + |
| 94 | + def _target() -> None: |
| 95 | + nonlocal run_error |
| 96 | + try: |
| 97 | + h.run() |
| 98 | + except BaseException as exc: # pragma: no cover |
| 99 | + run_error = exc |
| 100 | + finally: |
| 101 | + finished.set() |
| 102 | + |
| 103 | + thread = threading.Thread(target=_target, name="linopy-highs-run", daemon=True) |
| 104 | + thread.start() |
| 105 | + |
| 106 | + try: |
| 107 | + while not finished.wait(0.1): |
| 108 | + pass |
| 109 | + except KeyboardInterrupt: |
| 110 | + cancel_solve = getattr(h, "cancelSolve", None) |
| 111 | + if callable(cancel_solve): |
| 112 | + with contextlib.suppress(Exception): |
| 113 | + cancel_solve() |
| 114 | + while not finished.wait(0.1): |
| 115 | + pass |
| 116 | + raise |
| 117 | + |
| 118 | + if run_error is not None: |
| 119 | + raise run_error |
| 120 | + finally: |
| 121 | + if old_handle_keyboard_interrupt is not None: |
| 122 | + h.HandleKeyboardInterrupt = old_handle_keyboard_interrupt |
| 123 | + if old_handle_user_interrupt is not None: |
| 124 | + h.HandleUserInterrupt = old_handle_user_interrupt |
| 125 | + |
| 126 | + |
59 | 127 | # the first available solver will be the default solver |
60 | 128 | with contextlib.suppress(ModuleNotFoundError): |
61 | 129 | import gurobipy |
@@ -912,7 +980,7 @@ def _solve( |
912 | 980 | elif warmstart_fn: |
913 | 981 | h.readBasis(path_to_string(warmstart_fn)) |
914 | 982 |
|
915 | | - h.run() |
| 983 | + _run_highs_with_keyboard_interrupt(h) |
916 | 984 |
|
917 | 985 | condition = h.getModelStatus() |
918 | 986 | termination_condition = CONDITION_MAP.get( |
|
0 commit comments