Skip to content

Commit f64b26e

Browse files
authored
Merge pull request #2632 from openWB/revert-2629-thread-handling
Revert "use own exception class in @exit_after"
2 parents 818ee1d + 27e1428 commit f64b26e

2 files changed

Lines changed: 22 additions & 34 deletions

File tree

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,26 @@
1+
import _thread as thread
12
from threading import Timer
2-
import threading
3+
import sys
34

45

5-
class TimeoutException(Exception):
6-
pass
6+
def quit_function(fn_name):
7+
sys.stderr.flush() # Python 3 stderr is likely buffered.
8+
thread.interrupt_main() # raises KeyboardInterrupt
79

810

911
def exit_after(s):
10-
'''
11-
Nutze als Decorator, um einen Timeout für eine Funktion zu erzwingen.
12-
Wirft TimeoutException, wenn die Funktion länger als s Sekunden benötigt.
13-
Basiert auf https://stackoverflow.com/questions/492519/timeout-on-a-function-call.
12+
''' https://stackoverflow.com/questions/492519/timeout-on-a-function-call
13+
use as decorator to exit process if
14+
function takes longer than s seconds
1415
'''
1516
def outer(fn):
1617
def inner(*args, **kwargs):
17-
timer = Timer(
18-
s,
19-
lambda: thread_raise(TimeoutException(f"Timeout nach {s} Sekunden in {fn.__name__}")))
18+
timer = Timer(s, quit_function, args=[fn.__name__])
2019
timer.start()
2120
try:
22-
return fn(*args, **kwargs)
21+
result = fn(*args, **kwargs)
2322
finally:
2423
timer.cancel()
24+
return result
2525
return inner
2626
return outer
27-
28-
29-
def thread_raise(ex):
30-
# Raise Exception im aktuellen Thread (nur für Hauptthread zuverlässig)
31-
import ctypes
32-
tid = threading.get_ident()
33-
ctypes.pythonapi.PyThreadState_SetAsyncExc(ctypes.c_long(tid), ctypes.py_object(ex))

packages/main.py

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,10 @@
33
"""
44
# flake8: noqa: E402
55
import logging
6-
import threading
7-
import sys
86
from helpermodules import logger
97
from helpermodules.utils import run_command, thread_handler
10-
from helpermodules.utils._exit_after import TimeoutException
8+
import threading
9+
import sys
1110

1211
# als erstes logging initialisieren, damit auch ImportError geloggt werden
1312
logger.setup_logging()
@@ -46,9 +45,9 @@ def __init__(self):
4645
def handler10Sec(self):
4746
""" führt den Algorithmus durch.
4847
"""
49-
@exit_after(data.data.general_data.data.control_interval)
50-
def handler_with_control_interval():
51-
try:
48+
try:
49+
@exit_after(data.data.general_data.data.control_interval)
50+
def handler_with_control_interval():
5251
if (data.data.general_data.data.control_interval / 10) == self.interval_counter:
5352
data.data.copy_data()
5453
loadvars_.get_values()
@@ -70,10 +69,6 @@ def handler_with_control_interval():
7069
self.interval_counter = 1
7170
else:
7271
self.interval_counter = self.interval_counter + 1
73-
except Exception:
74-
log.exception("Fehler im Main-Modul 10s-Handler")
75-
76-
try:
7772
log.info("# ***Start*** ")
7873
log.debug(run_command.run_shell_command("top -b -n 1 | head -n 20"))
7974
log.debug(f'Drosselung: {run_command.run_shell_command("if which vcgencmd >/dev/null; then vcgencmd get_throttled; else echo not found; fi")}')
@@ -91,7 +86,7 @@ def handler_with_control_interval():
9186
logging.debug(line.strip())
9287
Pub().pub("openWB/set/system/time", timecheck.create_timestamp())
9388
handler_with_control_interval()
94-
except TimeoutException:
89+
except KeyboardInterrupt:
9590
log.critical("Ausführung durch exit_after gestoppt: "+traceback.format_exc())
9691
except Exception:
9792
log.exception("Fehler im Main-Modul")
@@ -109,7 +104,7 @@ def handler5MinAlgorithm(self):
109104
data.data.general_data.grid_protection()
110105
data.data.optional_data.ocpp_transfer_meter_values()
111106
data.data.counter_all_data.validate_hierarchy()
112-
except TimeoutException:
107+
except KeyboardInterrupt:
113108
log.critical("Ausführung durch exit_after gestoppt: "+traceback.format_exc())
114109
except Exception:
115110
log.exception("Fehler im Main-Modul")
@@ -144,7 +139,7 @@ def handler5Min(self):
144139
general_internal_chargepoint_handler.internal_chargepoint_handler.heartbeat = False
145140
with ChangedValuesContext(loadvars_.event_module_update_completed):
146141
sub.system_data["system"].update_ip_address()
147-
except TimeoutException:
142+
except KeyboardInterrupt:
148143
log.critical("Ausführung durch exit_after gestoppt: "+traceback.format_exc())
149144
except Exception:
150145
log.exception("Fehler im Main-Modul")
@@ -156,7 +151,7 @@ def handler_midnight(self):
156151
thread_errors_path = Path(Path(__file__).resolve().parents[1]/"ramdisk"/"thread_errors.log")
157152
with thread_errors_path.open("w") as f:
158153
f.write("")
159-
except TimeoutException:
154+
except KeyboardInterrupt:
160155
log.critical("Ausführung durch exit_after gestoppt: "+traceback.format_exc())
161156
except Exception:
162157
log.exception("Fehler im Main-Modul")
@@ -165,7 +160,7 @@ def handler_midnight(self):
165160
def handler_random_nightly(self):
166161
try:
167162
data.data.system_data["system"].thread_backup_and_send_to_cloud()
168-
except TimeoutException:
163+
except KeyboardInterrupt:
169164
log.critical("Ausführung durch exit_after gestoppt: "+traceback.format_exc())
170165
except Exception:
171166
log.exception("Fehler im Main-Modul")
@@ -177,7 +172,7 @@ def handler_hour(self):
177172
for cp in data.data.cp_data.values():
178173
calculate_charge_cost(cp)
179174
data.data.optional_data.et_get_prices()
180-
except TimeoutException:
175+
except KeyboardInterrupt:
181176
log.critical("Ausführung durch exit_after gestoppt: "+traceback.format_exc())
182177
except Exception:
183178
log.exception("Fehler im Main-Modul")

0 commit comments

Comments
 (0)