-
Notifications
You must be signed in to change notification settings - Fork 0
/
thread_manager.py
110 lines (98 loc) · 3.96 KB
/
thread_manager.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
# -*- coding: utf-8 -*-
"""
-------------------------------------------------
File Name: startThread
Description :
Author : HSH
date: 2017/11/13
-------------------------------------------------
Change Activity:
2017/11/14:
-------------------------------------------------
"""
__author__ = 'HSH'
# 读取配置文件
import configparser
import requests
cf = configparser.ConfigParser()
cf.read('startThread.conf', encoding='UTF-8')
thread_list = cf['thread_list']
def start_threads():
for key in thread_list.keys():
start_thread(key)
def query_state():
open_thread = []
close_thread = []
for key in thread_list.keys():
try:
data = {'className': cf[key]['classname']}
url = 'http://localhost:' + cf[key]['port'] + '/enavpre/service/v1/ThreadManager/queryState'
print(url)
r = requests.get(url, data)
if r.status_code == 303:
open_thread.append(key)
elif r.status_code == 304:
close_thread.append(key)
except Exception:
print("连接tomcat" + cf[key]['port'] + "异常")
print("开启中线程为:\n")
num = 0
for k in open_thread:
print(str(num) + "." + thread_list[k] + "\n")
num = num + 1
print("关闭的线程为:\n")
num = 0
for k in close_thread:
print(str(num) + "." + thread_list[k] + "\n")
num = num + 1
if input("是否需要改变状态?(y/n)") == 'y':
arg_q = True
while arg_q:
arg_q = input("\"exit\"-退出修改模式模式\n\"close,x\"-关闭编号为x的线程\n\"start,x\"-开启编号为x的线程\n请输入指令:")
if arg_q == "exit":
break
arg_q = arg_q.split(",")
if len(arg_q) != 2:
print("参数有误请重新输入!")
arg_q = True
else:
if arg_q[0] == "close":
shut_thread(open_thread[int(arg_q[1])])
elif arg_q[0] == "start":
start_thread(close_thread[int(arg_q[1])])
def shut_thread(class_simnple_name):
data = {'className': cf[class_simnple_name]['classname']}
url = 'http://localhost:' + cf[class_simnple_name]['port'] + '/enavpre/service/v1/ThreadManager/shutDown'
r = requests.get(url, data)
if r.status_code == 304:
print(cf['thread_list'][class_simnple_name] + "关闭成功,并且端口号为" + cf[class_simnple_name][
'port'] + "的tomcat说" + r.text)
else:
print(cf['thread_list'][class_simnple_name] + "关闭失败,未知错误")
def start_thread(class_simnple_name):
data = {'className': cf[class_simnple_name]['classname'], 'sleepTime': cf[class_simnple_name]['sleepTime']}
url = 'http://localhost:' + cf[class_simnple_name]['port'] + '/enavpre/service/v1/ThreadManager/startRun'
r = requests.get(url, data)
if r.status_code == 200:
print(cf['thread_list'][class_simnple_name] + "启动成功,并且端口号为" + cf[class_simnple_name][
'port'] + "的tomcat说" + r.text)
elif r.status_code == 400:
print(cf['thread_list'][class_simnple_name] + "启动失败,并且端口号为" + cf[class_simnple_name][
'port'] + "的tomcat说" + r.text + ",详情请查看tomcat日志")
elif r.status_code == 201:
print(cf['thread_list'][class_simnple_name] + "重启成功,并且端口号为" + cf[class_simnple_name][
'port'] + "的tomcat说" + r.text)
def main():
arg = True
while arg:
arg = input("\"startall\"-启动所有线程\n\"query\"-查询已配置线程状态\n\"exit\"-退出程序\n请输入指令:")
if arg == "exit":
exit()
elif arg == "startall":
start_threads()
elif arg == "query":
query_state()
else:
print("参数有误请重新输入")
if __name__ == '__main__':
main()