-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththreadManager.py
430 lines (318 loc) · 12.7 KB
/
threadManager.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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
import sys
import time
import atexit
import logging
import threading
class ThreadManager():
def __init__(self, maxThreads=50):
""" Manages a group of threads.
maxThreads (int) - How many threads can run at once
~ Crashes around 600
"""
self.maxThreads = maxThreads
self.catalogue = {}
self.lock = threading.RLock()
def get(self, label):
""" Returns the thread if it is being managed by this threadManager.
label (str) - What the child is labeled as
Example Input: get("autoSave")
"""
if (label is None):
return
with self.lock:
return self.catalogue.get(label)
def add(self, child, *, canReplace=False):
""" Returns the thread if it is being managed by this threadManager.
See: https://stackoverflow.com/questions/70733909/can-i-run-cleanup-code-in-daemon-threads-in-python/70737935#70737935
child (MyThread) - What to add
canReplace (bool) - Determines if 'child' can replace another one with the same label
Example Input: add("autoSave")
"""
if (child is None):
return
with self.lock:
label = child.label
if (child.daemon):
# Ensure threads are cleaned up on exit
atexit.register(lambda: child.join())
if ((not canReplace) and (label in self.catalogue)):
errorMessage = f"A Thread is already labeled as '{label}'"
raise KeyError(errorMessage)
thread = self.catalogue.get(label)
if (thread is not None):
thread.stop()
thread.join()
self.catalogue[label] = child
def remove(self, child):
""" Removes a child from this threadManager.
child (object) - What to remove
Example remove(self)
Example remove("autoSave")
"""
with self.lock:
if (isinstance(child, str)):
self.catalogue.pop(child, None)
return
self.catalogue.pop(child.label, None)
def run(self, myFunction, *args, daemon=True, **kwargs):
""" Runs a function in the background in a way that it does not lock up the GUI.
Meant for functions that take a long time to run.
If 'makeThread' is true, the new thread object will be returned to the user.
Example Input: run(self.startupFunction)
"""
return self.MyThread(self, myFunction, *args, daemon=daemon, **kwargs)
def listen(self, myFunction, *args, listenFunction=True, **kwargs):
return self.MyThread(self, myFunction, *args, listenFunction=listenFunction, **kwargs)
def oneShot(self, myFunction, *args, listenFunction=True, alternateFunction=True, **kwargs):
return self.MyThread(self, myFunction, *args, listenFunction=listenFunction, alternateFunction=alternateFunction, **kwargs)
class MyThread(threading.Thread):
""" Used to run functions in the background.
More information on threads can be found at: https://docs.python.org/3.4/library/threading.html
Use: https://wiki.wxpython.org/Non-Blocking%20Gui
Use: http://effbot.org/zone/thread-synchronization.htm
_________________________________________________________________________
CREATE AND RUN A NEW THREAD
#Create new threads
thread1 = myThread(1, "Thread-1", 1)
thread2 = myThread(2, "Thread-2", 2)
#Start new threads
thread1.start()
thread2.start()
_________________________________________________________________________
If you exit the main thread, the other threads will still run.
EXAMPLE CREATING A THREAD THAT EXITS WHEN THE MAIN THREAD EXITS
If you want the created thread to exit when the main thread exits, make it a daemon thread.
thread1 = myThread(1, "Thread-1", 1, daemon = True)
You can also make it a daemon using the function:
thread1.setDaemon(True)
_________________________________________________________________________
CLOSING A THREAD
If any thread is open, the program will not end. To close a thread use return on the function that is running in the thread.
The thread will then close itself automatically.
"""
def __init__(self, parent, myFunction, label=None, *args, daemon=None, name=None, autoStart=True,
delay_checkClear=10, delay_listen=1000,
myFunctionArgs=None, myFunctionKwargs=None,
preStartFunction=None, preStartFunctionArgs=None, preStartFunctionKwargs=None,
postStartFunction=None, postStartFunctionArgs=None, postStartFunctionKwargs=None,
preFunction=None, preFunctionArgs=None, preFunctionKwargs=None,
postFunction=None, postFunctionArgs=None, postFunctionKwargs=None,
stopFunction=None, stopFunctionArgs=None, stopFunctionKwargs=None,
listenFunction=None, listenFunctionArgs=None, listenFunctionKwargs=None,
alternateFunction=None, alternateFunctionArgs=None, alternateFunctionKwargs=None, **kwargs):
""" Setup the thread.
name (str) - The thread name. By default, a unique name is constructed of the form "Thread-N" where N is a small decimal number.
daemon (bool) - Sets whether the thread is daemonic. If None (the default), the daemonic property is inherited from the current thread.
label (str) - A name for the ThreadManager
~ If name already exists: Will stop the existing thread and replace it with this one
myFunction (function) - The main function to run on the thread
preStartFunction (function) - A function that runs before the thread starts
postStartFunction (function) - A function that runs after the thread starts
preFunction (function) - A function that runs before the thread runs *myFunction*
postFunction (function) - A function that runs after the thread has finished running *myFunction*
stopFunction (function) - An extra function used to stop the thread
listenFunction (function) - A function to run for checking if *myFunction* should run again
~ Must return True before *myFunction* can run the first time
~ If given, will keep the thread alive even after *myFunction* runs
alternateFunction (function) - A function to run instead of *myFunction* if it has run already (unless *reset* is called)
~ If not given, the thread will not have a 'One Shot' behavior
~ Can be true to make it be a 'One Shot' without providing a function
Example Input: MyThread(self, myFunction)
Example Input: MyThread(self, myFunction, name="Thread-1")
Example Input: MyThread(self, myFunction, daemon=True)
"""
# Initialize the thread
threading.Thread.__init__(self, name=name, daemon=daemon)
# Setup thread properties
self.event_stop = threading.Event() # Used to stop the thread
self.event_noPause = threading.Event() # Used to pause the thread
self.event_triggered = threading.Event() # Used to give 'One Shot' behavior to the thread
# Initialize internal variables
self.parent = parent
self.label = label or name
self.delay_listen = delay_listen
self.delay_checkClear = delay_checkClear
self.myFunction = myFunction
self.myFunctionArgs = myFunctionArgs or ()
self.myFunctionKwargs = myFunctionKwargs or {}
self.postFunction = postFunction
self.postFunctionArgs = postFunctionArgs or ()
self.postFunctionKwargs = postFunctionKwargs or {}
self.preFunction = preFunction
self.preFunctionArgs = preFunctionArgs or ()
self.preFunctionKwargs = preFunctionKwargs or {}
self.preStartFunction = preStartFunction
self.preStartFunctionArgs = preStartFunctionArgs or ()
self.preStartFunctionKwargs = preStartFunctionKwargs or {}
self.postStartFunction = postStartFunction
self.postStartFunctionArgs = postStartFunctionArgs or ()
self.postStartFunctionKwargs = postStartFunctionKwargs or {}
self.stopFunction = stopFunction
self.stopFunctionArgs = stopFunctionArgs or ()
self.stopFunctionKwargs = stopFunctionKwargs or {}
self.listenFunction = listenFunction
self.listenFunctionArgs = listenFunctionArgs or ()
self.listenFunctionKwargs = listenFunctionKwargs or {}
self.alternateFunction = alternateFunction
self.alternateFunctionArgs = alternateFunctionArgs or ()
self.alternateFunctionKwargs = alternateFunctionKwargs or {}
self._checkClear()
self.parent.add(self, canReplace=True)
if (autoStart):
self.start()
def start(self, *args, **kwargs):
try:
if (self.preStartFunction):
self.preStartFunction(*self.preStartFunctionArgs, **self.preStartFunctionKwargs)
except Exception as error:
self.parent.remove(self)
raise error
threading.Thread.start(self, *args, **kwargs)
def _checkClear(self):
""" Waits for other threads to stop if there are too many running."""
if (threading.active_count() <= self.parent.maxThreads):
return
# warnings.warn(f"Too many threads at {printCurrentTrace(printout = False)}", Warning, stacklevel = 2)
while (threading.active_count() > self.parent.maxThreads):
time.sleep(self.delay_checkClear / 1000)
def _triggerFunction(self):
if (self.preFunction):
self.preFunction(*self.preFunctionArgs, **self.preFunctionKwargs)
if (self.event_stop.is_set()):
return
self.myFunction(*self.myFunctionArgs, **self.myFunctionKwargs)
self.event_triggered.set()
if (self.event_stop.is_set()):
return
if (self.postFunction):
self.postFunction(*self.postFunctionArgs, **self.postFunctionKwargs)
def _checkPause(self, timeout=None):
if (not self.event_noPause.is_set()):
return
if (not timeout):
self.event_noPause.wait()
return
while True:
self.event_noPause.wait(timeout=timeout)
if (self.event_noPause.is_set()):
break
def run(self):
""" Runs the thread and then closes it.
If *listenFunction* was given, will keep the. thread alive until it is explicitly stopped
To start the thread correctly, use start()
"""
logging.info(f"Running Thread '{self.name}'; total: {threading.active_count()}")
is_oneshot = isinstance(self.alternateFunction, bool)
is_listener = isinstance(self.listenFunction, bool)
try:
if (self.event_stop.is_set()):
return
if (self.postStartFunction):
self.postStartFunction(*self.postStartFunctionArgs, **self.postStartFunctionKwargs)
if (self.event_stop.is_set()):
return
# Account for no listener
if (self.listenFunction is None):
return self._triggerFunction()
while True:
time.sleep(self.delay_listen / 1000)
if (self.event_stop.is_set()):
return
self._checkPause()
answer = True if is_listener else self.listenFunction(*self.listenFunctionArgs, **self.listenFunctionKwargs)
if (not answer):
if (self.event_stop.is_set()):
return
continue
# Account for One Shot
if (self.alternateFunction and self.event_triggered.is_set()):
if (not is_oneshot):
self.alternateFunction(*self.alternateFunctionArgs, **self.alternateFunctionKwargs)
if (self.event_stop.is_set()):
return
continue
self._triggerFunction()
if (self.event_stop.is_set()):
return
except Exception as error:
raise error
finally:
self.parent.remove(self)
logging.info(f"Closing Thread '{self.name}'; total: {threading.active_count()}")
def stop(self):
""" Marks the thread as needing to stop."""
self.event_stop.set()
if (self.stopFunction):
self.stopFunction(*self.stopFunctionArgs, **self.stopFunctionKwargs)
def join(self, *args, **kwargs):
self.stop()
threading.Thread.join(self, *args, **kwargs)
def pause(self, state=True):
if (state):
self.event_noPause.clear()
else:
self.event_noPause.set()
def reset(self):
self.event_triggered.clear()
rootManager = ThreadManager()
if (__name__ == "__main__"):
def test_run():
def test():
for i in range(10):
print("@test", i)
time.sleep(250 / 1000)
def postFunction():
nonlocal stopLoop
stopLoop = True
######################################
rootManager.run(test, postFunction=postFunction)
stopLoop = False
while (not stopLoop):
print("@__main__")
time.sleep(500 / 1000)
def test_trigger():
def test():
for i in range(10):
print("@test", i)
time.sleep(250 / 1000)
def postFunction():
nonlocal stopLoop
stopLoop = True
def listenFunction():
print("@listenFunction")
return canFire
######################################
rootManager.run(test, listenFunction=listenFunction, postFunction=postFunction)
canFire = False
stopLoop = False
print("@__main__.1")
time.sleep(3)
canFire = True
while (not stopLoop):
print("@__main__.2")
time.sleep(500 / 1000)
def test_listen():
def test():
for i in range(10):
print("@test", i)
time.sleep(250 / 1000)
def postFunction():
nonlocal count
count += 1
def listenFunction():
print("@listenFunction")
return canFire
######################################
rootManager.run(test, listenFunction=listenFunction, postFunction=postFunction)
count = 0
while (count < 3):
print("@__main__")
canFire = False
time.sleep(1)
canFire = True
time.sleep(1)
##################################################
logging.basicConfig(level=logging.INFO)
# test_run()
# test_trigger()
test_listen()