-
Notifications
You must be signed in to change notification settings - Fork 0
/
fctthread.py
280 lines (236 loc) · 7.92 KB
/
fctthread.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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
"""Control methods in separate threads."""
import logging
import os
import queue
import subprocess
import sys
import threading
from collections.abc import Callable, Sequence
from typing import Any
__version__ = '0.2.23'
logger = logging.getLogger(__name__)
#-------------------------------------------------------
_ENCODING = 'utf-8'
def _popen_ext(cmd: str|Sequence[str], shell=False):
subprocess.Popen(cmd, shell=shell, start_new_session=True,
stdin=subprocess.DEVNULL, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
if sys.platform.startswith('linux'):
def _start_file(cmd: str) -> None:
_popen_ext(('xdg-open', cmd))
elif sys.platform.startswith('win'):
_ENCODING = 'cp850'
def _start_file(cmd: str) -> None:
os.startfile(cmd)
else:
logger.warning('fctthread not fully supported on %s', sys.platform)
def _start_file(cmd: str) -> None:
raise NotImplementedError(f'_start_file not implemented on {sys.platform}')
def shell_cmd(cmd: str|Sequence[str]) -> str:
"""Execute command and return stdout.
- cmd can be a string or a iterable containing args: (exe, arg1, ...)
- stderr is redirected to stdout
"""
return subprocess.run(cmd, shell=isinstance(cmd, str), encoding=_ENCODING, errors='replace',
stdin=subprocess.DEVNULL, stdout=subprocess.PIPE, stderr=subprocess.STDOUT).stdout
def start_app(cmd: str|Sequence[str]) -> bool:
"""Start application or open file."""
try:
if not isinstance(cmd, str):
_popen_ext(cmd)
elif os.path.isfile(cmd):
_start_file(cmd)
else:
_popen_ext(cmd, shell=True)
return True
except Exception:
logger.exception('not possible to start app, cmd: %s', cmd)
return False
def start_daemon(target: Callable, args: list|tuple = (), kwargs: dict|None = None) -> threading.Thread:
"""Start and return daemon thread."""
t = threading.Thread(target=target, args=args, kwargs=kwargs, daemon=True)
t.start()
return t
#-------------------------------------------------------
class _FakeThread:
def __init__(self, target: Callable|None = None, *, daemon: bool = True, activate: bool = False):
self._target = target
if not daemon:
raise ValueError('FakeThread must be daemonized')
self._state = threading.Event()
if not activate:
self._state.set()
def run(self) -> None:
self._state.clear()
try:
self._target()
finally:
self._state.set()
def join(self, timeout: float|None = None) -> None:
self._state.wait(timeout)
def is_alive(self) -> bool:
return not self._state.is_set()
class ThreadLoop:
"""Class to control function in a daemon thread.
Loops over target() until calling stop or the target returns True.
"""
def __init__(self, target: Callable[[], bool|None]):
self._target = target
self._start_flag = False
self._should_run = False
self._stop_flag = False
self._t = _FakeThread()
self._lock = threading.Lock()
def _handle(self) -> None:
logger.debug('ThreadLoop start handle of %s', self._target)
while True:
try:
while self._should_run and not self._start_flag:
if self._target():
break
except:
logger.exception('ThreadLoop error calling target')
with self._lock:
if self._start_flag:
self._start_flag = False
self._should_run = True
else:
logger.debug('ThreadLoop stop handle of %s', self._target)
self._should_run = False
# stop flag is used for the rare case when this handle stops
# but the thread is still alive and start is called
self._stop_flag = True
return
def run(self) -> None:
"""Run target as loop in the thread of the caller.
Raise a RuntimeError if loop is already alive.
Can be stopped by a KeyboardInterrupt or calling stop() from an other thread.
"""
with self._lock:
if self._t.is_alive():
raise RuntimeError('ThreadLoop already active')
self._start_flag = True
self._stop_flag = False
self._t = _FakeThread(self._handle, activate=True)
self._t.run()
def start(self) -> None:
with self._lock:
self._start_flag = True
if self._stop_flag:
self._t.join()
elif self._t.is_alive():
return
self._stop_flag = False
self._t = threading.Thread(target=self._handle, daemon=True)
self._t.start()
def stop(self, timeout: float|None = None) -> bool:
with self._lock:
self._start_flag = False
self._should_run = False
self._t.join(timeout)
return not self._t.is_alive()
def join(self, timeout: float|None = None) -> None:
self._t.join(timeout)
def is_alive(self) -> bool:
return self._t.is_alive()
#-------------------------------------------------------
class QueueWorker:
"""Class to process elements from a queue in separate threads.
If a thread is not called within timeout seconds it will be stopped.
"""
def __init__(self, target: Callable[[Any], None],
maxthreads: int = 2, *, timeout: float = 10.0, qsize: int|None = None):
if maxthreads <= 0:
raise ValueError('number of threads must be at least 1')
if timeout < 0.0:
raise ValueError('timeout must be nonnegative')
self._target = target
self._maxthreads = maxthreads
self.timeout = timeout
self._enabled = False
self._active_loops = 0
self._q = queue.Queue(qsize or maxthreads)
self._lock = threading.Lock()
self._all_done = threading.Condition(self._lock)
def _handle(self) -> None:
while True:
try:
while self._enabled:
arg = self._q.get(timeout=self.timeout)
try:
self._target(arg)
except:
logger.exception('QueueWorker error calling target with %s', arg)
finally:
self._q.task_done()
except queue.Empty:
pass
finally:
with self._lock:
if not self._enabled or self._active_loops > self._q.unfinished_tasks:
self._active_loops -= 1
self._all_done.notify_all()
return
def _start_thread(self) -> None:
assert self._lock.locked()
if self._enabled and self._active_loops < self._maxthreads:
threading.Thread(target=self._handle, daemon=True).start()
self._active_loops += 1
def put(self, arg: Any, timeout: float|None = None) -> None:
self._q.put(arg, timeout=timeout)
with self._lock:
if self._active_loops < self._q.unfinished_tasks:
self._start_thread()
def start(self) -> None:
with self._lock:
if self._enabled:
return
self._enabled = True
for _ in range(self._q.qsize()):
self._start_thread()
def stop(self, timeout: float|None = None) -> bool:
with self._lock:
self._enabled = False
self.join(timeout)
return not self._active_loops
def join(self, timeout: float|None = None) -> None:
with self._all_done:
self._all_done.wait_for(lambda: self._active_loops<=0, timeout)
def is_alive(self) -> bool:
return self._enabled or self._active_loops > 0
def info(self) -> dict[str, Any]:
return {'enabled': self._enabled, 'loops': self._active_loops,
'unfinished': self._q.unfinished_tasks, 'waiting': self._q.qsize()}
class CmpEvent:
"""Class to receive data from another thread after a successful comparison.
This data is accessible in the variable result.
An optional answer can be sent to the compare thread.
"""
def __init__(self, cmpfct: Callable[[Any, Any], bool] = lambda x,y: x==y):
"""Init method accepts an alternative boolean compare function:
cmpfct(init_value, compare_value), equality check (==) by default.
"""
self._cmpfct = cmpfct
self.result = None
self._cmpval = None
self._answer = None
self._waiting = False
self._cond = threading.Condition(threading.Lock())
def init(self, cmpval: Any, answer: Any = True) -> None:
with self._cond:
self.result = None
self._cmpval = cmpval
self._answer = answer
self._waiting = True
def wait(self, timeout: float|None = None) -> bool:
"""Return False while waiting for a match, True otherwise."""
with self._cond:
return self._cond.wait(timeout) if self._waiting else True
def compare(self, cmpval: Any, result: Any) -> Any:
if self._waiting:
with self._cond:
if self._cmpfct(self._cmpval, cmpval):
self.result = result
self._waiting = False
self._cond.notify_all()
return self._answer
return None