-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththreads.py
62 lines (55 loc) · 1.89 KB
/
threads.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
import threading
import sys
from traceback import print_tb,print_exc
from random import randint
from time import ctime as now
class JobThread(threading.Thread):
"""
Thread that executes a job every N milliseconds
"""
def __init__(self, func, ref):
threading.Thread.__init__(self)
self._finished = threading.Event()
self._func = func
self._ref = ref
self._error = False
def copy(self):
return self.__class__(self._func, self._ref)
def shutdown(self):
"""
Stop this thread
"""
self._finished.set()
def is_shutdown(self):
"""
Boolean check on the thread's state
"""
return bool(self._finished.isSet())
def run(self):
"""
Keep running this thread until it's shutdown
"""
self._finished.wait(10)
while not self._finished.isSet():
try:
self._func(self._ref)
self._error = False
except:
if not self._error:
print(" ")
print(">>>Exception occured in thread: {0}".format(sys.exc_info()[1]))
print_tb(sys.exc_info()[2])
print(" ")
f = open('{0} - ThreadLog.txt'.format(self._ref.config['name']),'a')
f.write("\r\n")
f.write(now())
f.write("\r\nConnection: {0}\r\n".format(self._ref.config['host']))
print_exc(None,f)
f.write("\r\n")
f.close()
self._error = True
finally:
if self._func._max:
self._finished.wait(randint(self._func._min,self._func._max)*0.001)
else:
self._finished.wait(self._func._min*0.001)