-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_threads.py
81 lines (62 loc) · 2.51 KB
/
create_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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# create as many threads as you want
#handling Exception in threads
#stop threads
#get a return value from thread based on it's index
import threading
class StoppableThread(threading.Thread):
"""Thread class with a stop() method. The thread itself has to check
regularly for the stopped() condition."""
def __init__(self,*args, **kwargs):
super(StoppableThread, self).__init__(*args,**kwargs)
self._stop_event = threading.Event()
self.shutdown_flag = threading.Event()
def stop(self):
self._stop_event.set()
def stopped(self):
return self._stop_event.is_set()
class Thread_Exception(Exception):
pass
def handling_Exception_in_thread(shared_object, *args, **kwargs):
try:
shared_object['target'](*args, **kwargs)
except Exception as err:
shared_object['errors'] = err
except KeyboardInterrupt as krr:
shared_object['errors'] = krr
def create_threads(target,parameters,return_flag=False):
"""
parameters must be a dict {threads index type int start with 0:(threads args,) type tuple,..}
threads args must be inside tuple with a , at the end
to get a return value you must add two parameter to your function dict and index
index=thread index , and dict return value of a thread index
def your_func(*args,response=None,index=None):
response[index]=...
return response
"""
if not isinstance(parameters,dict):
return 'parameters must be a dict {threads index:threads args,..}'
try:
shared_obj = {'errors':'', 'target': target}
threads=[None]*len(parameters)
if return_flag:
response={}
for index,args in parameters.items():
threads[index] = StoppableThread(target =handling_Exception_in_thread,args=(shared_obj,*args,response,index))
threads[index].start()
else:
for index,args in parameters.items():
threads[index] = StoppableThread(target =handling_Exception_in_thread,args=(shared_obj,args))
threads[index].start()
for thread in threads:
thread.join()
if shared_obj['errors']:
for thread in threads:
thread.stop()
raise Thread_Exception(shared_obj['errors'])
if return_flag:
return response
except KeyboardInterrupt as e:
for thread in threads:
thread.stop()
shared_obj['errors'] = e
raise Thread_Exception(shared_obj['errors'])