|
14 | 14 |
|
15 | 15 | from __future__ import annotations |
16 | 16 |
|
17 | | -from concurrent.futures import Executor, ThreadPoolExecutor |
| 17 | +from threading import Thread |
18 | 18 | from time import perf_counter_ns, sleep |
19 | 19 | from typing import Callable, Generic, ItemsView, KeysView, Optional, TypeVar |
20 | 20 |
|
@@ -119,28 +119,26 @@ def __init__( |
119 | 119 | should_dispose_func: Optional[Callable] = None, |
120 | 120 | item_disposal_func: Optional[Callable] = None): |
121 | 121 | super().__init__(cleanup_interval_ns, should_dispose_func, item_disposal_func) |
122 | | - self._executor: Executor = ThreadPoolExecutor(thread_name_prefix="SlidingExpirationCacheWithCleanupThreadExecutor") |
123 | | - self.init_cleanup_thread() |
124 | | - |
125 | | - def init_cleanup_thread(self) -> None: |
126 | | - self._executor.submit(self._cleanup_thread_internal) |
| 122 | + self._cleanup_thread = Thread(target=self._cleanup_thread_internal, daemon=True) |
| 123 | + self._cleanup_thread.start() |
127 | 124 |
|
128 | 125 | def _cleanup_thread_internal(self): |
129 | | - logger.debug("SlidingExpirationCache.CleaningUp") |
130 | | - current_time = perf_counter_ns() |
131 | | - sleep(self._cleanup_interval_ns / 1_000_000_000) |
132 | | - self._cleanup_time_ns.set(current_time + self._cleanup_interval_ns) |
133 | | - keys = [key for key, _ in self._cdict.items()] |
134 | | - for key in keys: |
| 126 | + while True: |
135 | 127 | try: |
136 | | - self._remove_if_expired(key) |
| 128 | + sleep(self._cleanup_interval_ns / 1_000_000_000) |
| 129 | + logger.debug("SlidingExpirationCache.CleaningUp") |
| 130 | + self._cleanup_time_ns.set(perf_counter_ns() + self._cleanup_interval_ns) |
| 131 | + keys = [key for key, _ in self._cdict.items()] |
| 132 | + for key in keys: |
| 133 | + try: |
| 134 | + self._remove_if_expired(key) |
| 135 | + except Exception: |
| 136 | + pass # ignore |
137 | 137 | except Exception: |
138 | | - pass # ignore |
139 | | - |
140 | | - self._executor.shutdown() |
| 138 | + break |
141 | 139 |
|
142 | 140 | def _cleanup(self): |
143 | | - pass # do nothing, cleanup thread does the job |
| 141 | + pass # cleanup thread handles this |
144 | 142 |
|
145 | 143 |
|
146 | 144 | class CacheItem(Generic[V]): |
|
0 commit comments