-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocess_manager2.py
173 lines (150 loc) · 5.51 KB
/
process_manager2.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
# manages and organzies all the threads assigned to processes by the assistant
# all threads of a particular type are stored in stacks so that the user
# can close the most recent instance of a process, I'm really over engineering
# this as I doubt users will have more than one of a particular task open
# at a time, but just incase :P
import threading
from multiprocessing import Process
import time
from collections import deque
class PManager:
def __init__(self):
self.processes = {}
# adds a process of a given type(name) to the top of the process stack
# for its given type (explain better)
def add(self, name, process, flags):
if(self.processes.get(name)==None):
self.processes[name] = deque()
thread = managed_process(process,flags)
print('starting process')
thread.start()
self.processes[name].append(thread)
# adds a process of a given type to the bottom of the process stack
# for its given type
def addBottom(self, name, process, flags):
if(self.processes.get(name)==None):
self.processes[name] = deque()
thread = managed_process(process, flags)
thread.start()
self.processes[name].appendleft(thread)
# removes the top process in the stack for the give process name.
# returns False if their are no processes of that name,
# and True if the top process in the stack was properly removed.
def remove(self, name):
if self.processes.get(name)==None:
return False
toRemove = self.processes[name].pop()
toRemove.kill()
if(len(self.processes[name])==0):
del self.processes[name]
return True
# removes the nth process in the given process stack
# returns True if the process was removed
# returns False if n was to large or name was not found
def removeN(self, name, n):
if self.processes.get(name)==None or len(self.processes[name]) < n:
return False
tempStack = deque()
for i in range(n-1):
tempStack.append(self.processes[name].pop())
toRemove = self.processes[name].pop()
toRemove.kill()
for i in range(n-1):
self.processes[name].append(tempStack.pop())
return True
# removes a process
def removeId(self, name, id):
print('to do')
# get the top process on a specific process stack
def getProcess(self, name):
if self.processes.get(name) != None and len(self.processes[name]) > 0:
temp = self.processes.get(name).pop()
self.processes.get(name).append(temp)
return temp
return False
# class to manage killing threads using a pass by reference flag that can
# be changed to end the function. For this to work there must be proper
# flag logic in the passed function itself.
class thread_with_flag:
def __init__(self, func, flag = [False], id = -1):
self.func = func
self.flag = flag
self.t1 = threading.Thread(target = self.func, args = (self.flag,))
self.id = id
# changes the flag to True to tell the function to end its execution
# as quickly as possible.
def kill(self):
print('killing thread')
self.flag[0] = True
while(self.t1.is_alive()):
continue
self.t1.join()
# starts the execution of the thread's function.
def start(self):
self.t1.start()
# example of properly incorporated flag function logic.
# essentially the function needs a parameter for the flag
# and an if statement that will break out of any loops
# in the function. This is because the thread will terminate
# once the function is done executing.
def main1():
def run(stop):
while True:
print('thread running')
time.sleep(1)
print(stop[0])
if stop[0]==True:
break
print('thread ending')
#t1 = threading.Thread(target = run, args=(False,))
#print('starting thread')
#t1.start()
#print('thread has started')
thread = thread_with_flag(run, [False])
print('starting thread')
thread.start()
print('thread has started')
time.sleep(5)
thread.kill()
# due to differences between the multiprocessing package and threading package
#the class needed to manage the process is a lot simpler and kind of redundant.
class managed_process:
def __init__(self, func, flags = [False], id = -1):
self.func = func
self.flags = flags
self.p1 = Process(target = self.func, args = (self.flags,))
self.id = id
# changes the flag to True to tell the function to end its execution
# as quickly as possible.
def kill(self):
print('killing thread')
self.p1.terminate()
self.p1.join()
# starts the execution of the thread's function.
def start(self):
self.p1.start()
# example of properly incorporated flag function logic for multiprocessing
# as opposed to threading.
def main2():
def run(stop = [False]):
while True:
print('thread running')
time.sleep(1)
p1 = Process(target = run)
print('starting thread')
p1.start()
print('thread has started')
time.sleep(5)
p1.terminate()
p1.join()
print('----testing process----')
thread = managed_process(run)
print('starting thread')
thread.start()
print('thread has started')
time.sleep(5)
thread.kill()
def main():
print('----processes manager main----')
if __name__ == '__main__':
main2()