Repository navigation
Expand file tree
/
Copy paththreadCreator.py
More file actions
159 lines (124 loc) · 4.84 KB
/
Copy paththreadCreator.py
File metadata and controls
159 lines (124 loc) · 4.84 KB
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
import urwid
import threading
import time
import random
import time
threads = list()
tempi = list()
values = []
class GraphView(urwid.WidgetWrap):
palette = [
('body', 'black', 'light gray', 'standout'),
('header', 'white', 'dark red', 'bold'),
('screen edge', 'light blue', 'dark cyan'),
('main shadow', 'dark gray', 'black'),
('line', 'black', 'light gray', 'standout'),
('bg background','light gray', 'black'),
('bg 1', 'black', 'dark blue', 'standout'),
('bg 1 smooth', 'dark blue', 'black'),
('bg 2', 'black', 'dark cyan', 'standout'),
('bg 2 smooth', 'dark cyan', 'black'),
('button normal','light gray', 'dark blue', 'standout'),
('button select','white', 'dark green'),
('line', 'black', 'light gray', 'standout'),
('pg normal', 'white', 'black', 'standout'),
('pg complete', 'white', 'dark magenta'),
('pg smooth', 'dark magenta','black')
] #palette tema
def __init__(self, x):
urwid.WidgetWrap.__init__(self, self.main_window())
def update_graph(self):
max_value = max(values)
l = []
for n in range(len(values)):
if n & 1:
l.append([0,values[n]])
else:
l.append([values[n],0])
self.graph.set_data(l,max_value)
def button(self, t, fn):
w = urwid.Button(t, fn)
w = urwid.AttrWrap(w, 'button normal', 'button select')
return w
def exit_program(self, w):
raise urwid.ExitMainLoop()
def graph_controls(self):
testo = ""
for i in range(len(values)):
testo += "Thread_"+str(i) +": " + str(values[i]/float(1000)) + "s \n"
# setup mode radio buttons
self.mode_buttons = []
l = [urwid.Text(testo,align="center"),
urwid.Divider(),
self.button("Quit", self.exit_program ),
]
w = urwid.ListBox(urwid.SimpleListWalker(l))
return w
def main_window(self):
self.graph = urwid.BarGraph(['bg background','bg 1','bg 2'], satt={(1,0): 'bg 1 smooth', (2,0): 'bg 2 smooth'})
self.graph_wrap = urwid.WidgetWrap( self.graph )
vline = urwid.AttrWrap( urwid.SolidFill(u'\u2502'), 'line')
c = self.graph_controls()
w = urwid.Columns([('weight',2,self.graph_wrap),
('fixed',1,vline), c],
dividechars=1, focus_column=2)
w = urwid.AttrWrap(w,'body')
w = urwid.LineBox(w)
w = urwid.AttrWrap(w,'line')
return w
class GraphController:
def __init__(self):
self.view = GraphView(self)
# update the view
self.view.update_graph()
def main(self):
self.loop = urwid.MainLoop(self.view, self.view.palette)
self.loop.run()
class myThread (threading.Thread):
def __init__(self, threadID, name,counter):
threading.Thread.__init__(self)
self.threadID = threadID
self.name = name
self.counter = counter
def run(self):
tempo = -(int(round(time.time() * 1000)))
print("Starting " + self.name)
print_time(self.name, self.counter, 5)
print("exiting " + self.name)
tempo = tempo + int(round(time.time() * 1000))
values.insert(self.threadID, tempo)
def print_time(threadName, delay, counter):
while counter:
time.sleep(random.randint(1, delay))
print ("%s: %s" % (threadName, time.ctime(time.time())))
counter -= 1
class MyTerminal(urwid.WidgetWrap):
def __init__(self):
self.screen_text = urwid.Text("Inserisci numero thread da creare poi premi enter per far iniziare il programma")
self.prompt_text = urwid.Edit('Numero Thread: ', '')
self._w = urwid.Frame(header=urwid.Pile([urwid.Text('Crea Thread'),
urwid.Divider()]),
body=urwid.ListBox([self.screen_text]),
footer=self.prompt_text,
focus_part='footer')
def keypress(self, size, key):
if key == 'enter':
createThread(int(self.prompt_text.edit_text))
raise urwid.ExitMainLoop()
super(MyTerminal, self).keypress(size, key)
def createThread(nThread):
for i in range(nThread):
delay = 5
threadName = "Thread-"+str(i)
newThread = myThread(i, threadName, delay)
threads.append(newThread)#appending new thread
newThread.start()#starting new thread
for thread in threads:
thread.join()
def main():
my_term = MyTerminal()
urwid.MainLoop(my_term).run()
print ("Exiting Main Thread")
GraphController().main()
if '__main__'==__name__:
main()