Mercurial > code > home > repos > light9
annotate lib/background_loop.py @ 2318:264e04dee006
try more faders
author | drewp@bigasterisk.com |
---|---|
date | Thu, 01 Jun 2023 14:17:58 -0700 |
parents | 436a1fdbfe4a |
children |
rev | line source |
---|---|
2215 | 1 # dev copy |
2 import asyncio | |
3 import logging | |
4 import time | |
5 import traceback | |
6 from typing import Any, Awaitable, Callable, Union | |
7 | |
8 from prometheus_client import Gauge, Summary | |
2216
acf1b68d031a
fancier background_loop reporting for faders
drewp@bigasterisk.com
parents:
2215
diff
changeset
|
9 from light9.recentfps import RecentFps |
acf1b68d031a
fancier background_loop reporting for faders
drewp@bigasterisk.com
parents:
2215
diff
changeset
|
10 from braillegraph import horizontal_graph |
2215 | 11 |
2216
acf1b68d031a
fancier background_loop reporting for faders
drewp@bigasterisk.com
parents:
2215
diff
changeset
|
12 log = logging.getLogger('loop') |
2215 | 13 |
14 | |
15 # throw this away (when net_routes is rewritten) | |
16 def loop_forever_sync(func, sleep_period, up_metric): | |
17 first_run = True | |
18 while True: | |
19 try: | |
20 func(first_run) | |
21 up_metric.set(1) | |
22 first_run = False | |
23 except Exception as ex: | |
24 log.error(ex) | |
25 traceback.print_exc() | |
26 up_metric.set(0) | |
27 time.sleep(sleep_period) | |
28 | |
29 | |
30 _created = [] | |
31 | |
32 # todo: some tricky typing | |
33 F_RET = Any | |
34 F_KWARGS = Any | |
35 | |
36 UserAsyncFunc = Callable[..., # always called with at least kwarg first_run=bool | |
37 Awaitable[F_RET]] | |
38 UserSyncFunc = Callable[..., # see above | |
39 F_RET] | |
40 UserFunc = Union[UserAsyncFunc, UserSyncFunc] | |
41 | |
42 | |
43 class Loop: | |
44 | |
45 def __init__( | |
46 self, | |
47 func: UserFunc, | |
48 sleep_period: float, | |
49 metric_prefix: str, | |
2216
acf1b68d031a
fancier background_loop reporting for faders
drewp@bigasterisk.com
parents:
2215
diff
changeset
|
50 extra_sleep_on_error: float = 2, |
acf1b68d031a
fancier background_loop reporting for faders
drewp@bigasterisk.com
parents:
2215
diff
changeset
|
51 log_fps=False, |
2215 | 52 ): |
53 self.func = func | |
54 self.sleep_period = sleep_period | |
2216
acf1b68d031a
fancier background_loop reporting for faders
drewp@bigasterisk.com
parents:
2215
diff
changeset
|
55 self.extra_sleep_on_error = extra_sleep_on_error |
acf1b68d031a
fancier background_loop reporting for faders
drewp@bigasterisk.com
parents:
2215
diff
changeset
|
56 self.metric_prefix = metric_prefix |
2215 | 57 |
58 self.up_metric = Gauge(f'{metric_prefix}_up', 'not erroring') | |
59 self.call_metric = Summary(f'{metric_prefix}_calls', 'calls') | |
60 self.lastSuccessRun = 0 | |
61 self.everSucceeded = False | |
2216
acf1b68d031a
fancier background_loop reporting for faders
drewp@bigasterisk.com
parents:
2215
diff
changeset
|
62 self.succeeding = False |
acf1b68d031a
fancier background_loop reporting for faders
drewp@bigasterisk.com
parents:
2215
diff
changeset
|
63 self.fps = RecentFps() if log_fps else None |
acf1b68d031a
fancier background_loop reporting for faders
drewp@bigasterisk.com
parents:
2215
diff
changeset
|
64 self.lastFpsLog = 0 |
2215 | 65 |
66 async def call_func(self, first_run: bool, call_kwargs={}) -> F_RET: | |
67 with self.call_metric.time(): | |
68 if asyncio.iscoroutinefunction(self.func): | |
69 ret = await self.func(first_run=first_run, **call_kwargs) | |
70 else: | |
71 ret = self.func(first_run=first_run, **call_kwargs) | |
72 return ret | |
73 | |
74 async def _run(self): | |
75 self.first_run = True | |
2216
acf1b68d031a
fancier background_loop reporting for faders
drewp@bigasterisk.com
parents:
2215
diff
changeset
|
76 self.newlyFailing = True |
2215 | 77 while True: |
78 await self._runOne() | |
79 await asyncio.sleep(self.sleep_period) | |
80 | |
81 async def runNow(self, **kwargs: F_KWARGS) -> F_RET: | |
82 """unlike background runs, you get to pass more args and get your func's | |
83 return value. | |
84 """ | |
85 return await self._runOne(call_kwargs=kwargs) | |
86 | |
87 async def _runOne(self, call_kwargs={}): | |
2216
acf1b68d031a
fancier background_loop reporting for faders
drewp@bigasterisk.com
parents:
2215
diff
changeset
|
88 now = time.time() |
acf1b68d031a
fancier background_loop reporting for faders
drewp@bigasterisk.com
parents:
2215
diff
changeset
|
89 self._updateFps(now) |
2215 | 90 try: |
91 result = await self.call_func(self.first_run, call_kwargs) | |
92 self.lastSuccessRun = time.time() | |
2216
acf1b68d031a
fancier background_loop reporting for faders
drewp@bigasterisk.com
parents:
2215
diff
changeset
|
93 if self.fps: |
acf1b68d031a
fancier background_loop reporting for faders
drewp@bigasterisk.com
parents:
2215
diff
changeset
|
94 self.fps.mark() |
2215 | 95 self.up_metric.set(1) |
96 self.everSucceeded = True | |
2216
acf1b68d031a
fancier background_loop reporting for faders
drewp@bigasterisk.com
parents:
2215
diff
changeset
|
97 self.newlyFailing = True |
2215 | 98 self.first_run = False |
99 except Exception as ex: | |
2216
acf1b68d031a
fancier background_loop reporting for faders
drewp@bigasterisk.com
parents:
2215
diff
changeset
|
100 if self.newlyFailing: |
acf1b68d031a
fancier background_loop reporting for faders
drewp@bigasterisk.com
parents:
2215
diff
changeset
|
101 traceback.print_exc() |
acf1b68d031a
fancier background_loop reporting for faders
drewp@bigasterisk.com
parents:
2215
diff
changeset
|
102 self.newlyFailing = False |
acf1b68d031a
fancier background_loop reporting for faders
drewp@bigasterisk.com
parents:
2215
diff
changeset
|
103 else: |
acf1b68d031a
fancier background_loop reporting for faders
drewp@bigasterisk.com
parents:
2215
diff
changeset
|
104 log.error(ex) |
2215 | 105 self.up_metric.set(0) |
106 result = None | |
2216
acf1b68d031a
fancier background_loop reporting for faders
drewp@bigasterisk.com
parents:
2215
diff
changeset
|
107 self.succeeding = False |
acf1b68d031a
fancier background_loop reporting for faders
drewp@bigasterisk.com
parents:
2215
diff
changeset
|
108 |
acf1b68d031a
fancier background_loop reporting for faders
drewp@bigasterisk.com
parents:
2215
diff
changeset
|
109 await asyncio.sleep(self.extra_sleep_on_error) |
2215 | 110 # todo: something that reveals error ratio |
111 return result | |
112 | |
2216
acf1b68d031a
fancier background_loop reporting for faders
drewp@bigasterisk.com
parents:
2215
diff
changeset
|
113 def _updateFps(self, now: float): |
2218 | 114 # not sure i even want this- it's redundant with some metrics code |
2216
acf1b68d031a
fancier background_loop reporting for faders
drewp@bigasterisk.com
parents:
2215
diff
changeset
|
115 if self.fps is None: |
acf1b68d031a
fancier background_loop reporting for faders
drewp@bigasterisk.com
parents:
2215
diff
changeset
|
116 return |
acf1b68d031a
fancier background_loop reporting for faders
drewp@bigasterisk.com
parents:
2215
diff
changeset
|
117 if now < self.lastFpsLog + 5: |
acf1b68d031a
fancier background_loop reporting for faders
drewp@bigasterisk.com
parents:
2215
diff
changeset
|
118 return |
acf1b68d031a
fancier background_loop reporting for faders
drewp@bigasterisk.com
parents:
2215
diff
changeset
|
119 d = self.fps() |
acf1b68d031a
fancier background_loop reporting for faders
drewp@bigasterisk.com
parents:
2215
diff
changeset
|
120 y_hi = 1 / self.sleep_period |
acf1b68d031a
fancier background_loop reporting for faders
drewp@bigasterisk.com
parents:
2215
diff
changeset
|
121 if not d: |
acf1b68d031a
fancier background_loop reporting for faders
drewp@bigasterisk.com
parents:
2215
diff
changeset
|
122 return |
acf1b68d031a
fancier background_loop reporting for faders
drewp@bigasterisk.com
parents:
2215
diff
changeset
|
123 pts = [int(min(4, y / y_hi * 4)) for y in d['recents']] |
acf1b68d031a
fancier background_loop reporting for faders
drewp@bigasterisk.com
parents:
2215
diff
changeset
|
124 log.info(f'{self.metric_prefix} fps={d["average"]:3.1f} (req={y_hi:3.1f}) {horizontal_graph(pts)}') |
acf1b68d031a
fancier background_loop reporting for faders
drewp@bigasterisk.com
parents:
2215
diff
changeset
|
125 self.lastFpsLog = now |
acf1b68d031a
fancier background_loop reporting for faders
drewp@bigasterisk.com
parents:
2215
diff
changeset
|
126 |
2215 | 127 |
128 def loop_forever( | |
129 func: UserFunc, | |
130 sleep_period: float, | |
131 metric_prefix='background_loop', | |
132 up_metric=None, | |
133 call_metric=None, | |
2216
acf1b68d031a
fancier background_loop reporting for faders
drewp@bigasterisk.com
parents:
2215
diff
changeset
|
134 extra_sleep_on_error=2, |
acf1b68d031a
fancier background_loop reporting for faders
drewp@bigasterisk.com
parents:
2215
diff
changeset
|
135 log_fps=False, |
2215 | 136 ): |
137 """ | |
138 sleep_period is the sleep time after however long func takes to run | |
139 """ | |
140 if up_metric is not None or call_metric is not None: | |
141 raise NotImplementedError('remove old-style metrics') | |
142 | |
2216
acf1b68d031a
fancier background_loop reporting for faders
drewp@bigasterisk.com
parents:
2215
diff
changeset
|
143 loop = Loop(func, sleep_period, metric_prefix, extra_sleep_on_error, log_fps) |
2215 | 144 _created.append(asyncio.create_task(loop._run())) |
145 return loop |