-
Notifications
You must be signed in to change notification settings - Fork 1
/
threader.py
85 lines (62 loc) · 1.89 KB
/
threader.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
from __future__ import print_function
from threading import Thread
import time
import datetime
import os, fnmatch
import psutil
import csv
import sys
from queue import LifoQueue
cmd = LifoQueue()
def run_prog():
now = datetime.datetime.now()
c = cmd.get()
print("* " + now.strftime("%Y-%m-%d %H:%M:%S") + " >> " + c + " - STARTED")
os.system(c)
now = datetime.datetime.now()
print("* " + now.strftime("%Y-%m-%d %H:%M:%S") + " << " + c + " - FINISHED")
"""
if (cmd.qsize()==0):
print("THAT'S ALL, FOLKS!")
need to save treads IDs and check it
"""
if __name__ == "__main__":
#listOfFiles = os.listdir('.')
#pattern = "*TZ0dt-1min.csv"
#for entry in listOfFiles:
# if fnmatch.fnmatch(entry, pattern):
#newf = entry.replace("1min.csv","TZ0dt-1min.csv")
#cmd.put("python conv_tz.py " + entry + " 0 > " + newf)
# cmd.put("python conv_tf.py " + entry)
n_models_to_generate = sys.argv[1]
models_prefix = int(sys.argv[2])
models_bat = sys.argv[3]
i= int(n_models_to_generate)
while (i>0):
cmd.put(models_bat + " " +str(models_prefix+i))
i-=1
n=1 # correct that to 10 later
time.sleep(15)
while (n>0): #run initial 30
thread = Thread(target = run_prog)
thread.start()
time.sleep(5)
n-=1
# whil there is something in the stack and processor load <88%
while (cmd.qsize()>0):
time.sleep(5)
t = 5
util = 0
while (t>0): # we do it a few times and calc avg to be sure
util += psutil.cpu_percent()
time.sleep(t)
t-=1
util /= 5
now = datetime.datetime.now()
print(now.strftime("%Y-%m-%d %H:%M:%S") + ": CPU Utilization = " + str(util) + "% // tasks in stack: " + str(cmd.qsize()))
# start new processes while CPU utilization is less than 89
if util<90:
thread = Thread(target = run_prog)
thread.start()
time.sleep(10) # then do checks once per minute
print("Done execution, waiting for all threads to continue...")